Disable selected key combinations using VBA

isladogs

MVP / VIP
Local time
Today, 02:36
Joined
Jan 14, 2017
Messages
19,021
Hi

I've been working on a kiosk type form following a question from another forum user.

As part of this, I'm trying to disable selected key combinations in a form using the Form_KeyDown (KeyCode As Integer, Shift As Integer) event.

I have set the form KeyPreview = Yes

Specifically I want to block Alt+Tab & the use of the Windows key e.g. Win+E

For example, I know that code like the following will work

'disable key E
Code:
If KeyCode=vbKeyE Then KeyCode=0

'disable tab key
Code:
If KeyCode=vbKeyTab Then KeyCode=0

'disable Alt key
Code:
If KeyCode = vbKeyMenu Then KeyCode=0

'another way of disabling Alt key
Code:
If Shift = acAltMask Then KeyCode=0

However combining to block the Alt+Tab combination doesn't work:
Code:
If Shift = acAltMask Then
        If KeyCode = vbKeyTab Then
            Debug.Print "ALT + TAB keys pressed"
            KeyCode=0
        End If
    End If

Similarly this doesn't work:
Code:
If Shift = acAltMask And KeyCode = vbKeyTab Then
        Debug.Print "ALT + TAB keys pressed"
        KeyCode=0
    End If

Similarly I can't find any way of detecting the Windows key let alone blocking it

Are blocking either of these possible using VBA or failing that using Windows APIs

Can anyone help with this
 
Hi Doc

Thanks for these links.
I'd seen some of them in my own Google searches

Its certainly possible to achieve this using external software like SharpKeys or AutoHotKey but I'd prefer to do this using VBA if possible

Similarly converting VB.net or C# may be a step too far

Another way of doing this is a registry hack:

To disable the Windows key, follow these steps:
Click Start, click Run, type regedt32, and then click OK.
On the Windows menu, click HKEY_LOCAL_ MACHINE on Local Machine.
Double-click the System\CurrentControlSet\Control folder, and then click the Keyboard Layout folder.
On the Edit menu, click Add Value, type in Scancode Map, click REG_BINARY as the Data Type, and then click OK.
Type 00000000000000000300000000005BE000005CE000000000 in the Data field, and then click OK.
Close Registry Editor and restart the computer.


To enable the Windows key, follow these steps:
Click Start, click Run, type regedt32, and then click OK.
On the Windows menu, click HKEY_LOCAL_ MACHINE on Local Machine.
Double-click the System\CurrentControlSet\Control folder, and then click the Keyboard Layout folder.
Right-click the Scancode Map registry entry, and then click Delete. Click Yes.
Close Registry Editor and restart the computer.

Going back to VBA, I've now found out that the left Win key has key code 91 but I still can't trap
combinations of keys such as Win+E before the associated action fires - in that case open Explorer

Will keep trying
 
Last edited:
Just an update to say I have worked out how to do this.

It involves a new registry key so it does mean that any changes apply globally
E.g. disabling the Windows key

However of course the registry change is reversible

Further details to follow when I've finished this ...
 

Users who are viewing this thread

Back
Top Bottom