Disable Specific Keyboard Shortcuts

Statto

New Member
Joined
Jul 2, 2014
Messages
6
I found some great source code from a website (https://www.rondebruin.nl/win/s4/win012.htm) that enables you to disable the user's ability to use keyboard shortcuts. I've used the blocks of code that disable/enable all keyboard shortcuts — not just ones that are listed.

I'd like to adapt the code so that I can disable all keyboard shortcuts, but still list any exemptions at the bottom. For example, the following will block all attempted keyboard shortcuts, but how can I allow Ctrl + X, Ctrl + C, Ctrl + V, Alt + F8 and any others to be exempt?

Code:
[/COLOR][COLOR=#3366CC]Sub Disable_Keys()[/COLOR]
    Dim StartKeyCombination As Variant
    Dim KeysArray As Variant
    Dim Key As Variant
    Dim I As Long

    On Error Resume Next

   [COLOR=black] 'Shift key = "+"  (plus sign)
    'Ctrl key = "^"   (caret)
    'Alt key = "%"    (percent sign
    'We fill the array with this keys and the key combinations
    'Shift-Ctrl, Shift- Alt, Ctrl-Alt, Shift-Ctrl-Alt
[/COLOR]
    For Each StartKeyCombination In Array("+", "^", "%", "+^", "+%", "^%", "+^%")

        KeysArray = Array("{BS}", "{BREAK}", "{CAPSLOCK}", "{CLEAR}", "{DEL}", _
                    "{DOWN}", "{END}", "{ENTER}", "~", "{ESC}", "{HELP}", "{HOME}", _
                    "{INSERT}", "{LEFT}", "{NUMLOCK}", "{PGDN}", "{PGUP}", _
                    "{RETURN}", "{RIGHT}", "{SCROLLLOCK}", "{TAB}", "{UP}")

       [COLOR=black] 'Disable the StartKeyCombination key(s) with every key in the KeysArray[/COLOR]
        For Each Key In KeysArray
            Application.OnKey StartKeyCombination & Key, ""
        Next Key

        [COLOR=black]'Disable the StartKeyCombination key(s) with every other key[/COLOR]
        For I = 0 To 255
            Application.OnKey StartKeyCombination & Chr$(I), ""
        Next I

        [COLOR=black]'Disable the F1 - F15 keys in combination with the Shift, Ctrl or Alt key[/COLOR]
        For I = 1 To 15
            Application.OnKey StartKeyCombination & "{F" & I & "}", ""
        Next I

    Next StartKeyCombination


    [COLOR=black]'Disable the F1 - F15 keys[/COLOR]
    For I = 1 To 15
        Application.OnKey "{F" & I & "}", ""
    Next I

    [COLOR=black]'Disable the PGDN and PGUP keys[/COLOR]
    Application.OnKey "{PGDN}", ""
    Application.OnKey "{PGUP}", "" [COLOR=#3366CC]End Sub[/COLOR][COLOR=#333333]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top