Block / Disable Windows key and PrtSc key in Excel VBA 7.1 (Tried RegisterHotKey)

Fionatan

New Member
Joined
Nov 30, 2021
Messages
2
Platform
  1. Windows
Hi,
I'm trying to find a way to block Windows key and PrintScreen key in my Excel file (VBA 7.1). Since Application.OnKey method can't be used, my online research points to two solutions: 1) RegisterHotKeys, 2) Keyboard Hook. I've tried RegisterHotKeys since this seems to be the preferred method based on my online research. Pls let me know if there are any other solutions I should explore. I'd also like to share my code below on RegisterHotKeys for both keys that I'm trying to block (in this example, minimizing the window) - when I run it, my excel file hangs and I'm unsure where the error lies. Thank you so much for your help!

This is for Windows key:
VBA Code:
Const MOD_WIN = &H8
Const PM_REMOVE = &H1
Const WM_HOTKEY = &H312

Type POINTAPI
x As Long
y As Long
End Type

Type MSG
    hwnd As LongPtr
    Message As Long
    wParam As LongPtr
    lParam As LongPtr
    time As Long
    pt As POINTAPI
End Type

Declare PtrSafe Function RegisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare PtrSafe Function UnregisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long) As Long
Declare PtrSafe Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As LongPtr, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Declare PtrSafe Function WaitMessage Lib "user32" () As Long

Public bCancel As Boolean
Sub ProcessMessages()
    Dim Message As MSG
    Do While Not bCancel
    WaitMessage
    If PeekMessage(Message, Application.hwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
        WindowState = vbMinimized
    End If
    DoEvents
    Loop
End Sub

Sub SetHotKey()
    Dim ret As Long
    bCancel = False
    ret = RegisterHotKey(Application.hwnd, &HBFFF&, MOD_WIN, 0)
    ProcessMessages
End Sub

Sub UnsetHotKey()
    bCancel = True
    Call UnregisterHotKey(Application.hwnd, &HBFFF&)
End Sub

This is for PrintScreen key:

VBA Code:
Const PM_REMOVE = &H1
Const WM_HOTKEY = &H312
Const VK_SNAPSHOT = &H2C

Type POINTAPI
x As Long
y As Long
End Type

Type MSG
    hwnd As LongPtr
    Message As Long
    wParam As LongPtr
    lParam As LongPtr
    time As Long
    pt As POINTAPI
End Type

Declare PtrSafe Function RegisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare PtrSafe Function UnregisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long) As Long
Declare PtrSafe Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As LongPtr, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Declare PtrSafe Function WaitMessage Lib "user32" () As Long

Public bCancel As Boolean
Sub ProcessMessages()
    Dim Message As MSG
    Do While Not bCancel
    WaitMessage
    If PeekMessage(Message, Application.hwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
        WindowState = vbMinimized
    End If
    DoEvents
    Loop
End Sub

Sub SetHotKey()
    Dim ret As Long
    bCancel = False
    ret = RegisterHotKey(Application.hwnd, &HBFFE&, 0, VK_SNAPSHOT)
    ProcessMessages
End Sub

Sub UnsetHotKey()
    bCancel = True
    Call UnregisterHotKey(Application.hwnd, &HBFFE&)
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Do you just want to disable the keys or do you want to run code when pressing them ?

What do you mean by excel hangs ? Does the application window not minimize ? Are you running the code from excel or from the VBE ?
 
Upvote 0
Do you just want to disable the keys or do you want to run code when pressing them ?

What do you mean by excel hangs ? Does the application window not minimize ? Are you running the code from excel or from the VBE ?
Hi Jaafar, thanks for your reply!
I actually just want to disable the keys. I'm unsure how to disable them, so I'm running the code to minimize the window instead.
The application window didn't minimize and the excel file freezes. I'm running the code from excel.
 
Upvote 0
What happens if you introduce a MsgBox instead of WindowState = vbMinimized ?? Does the MsgBox appear when pressing the registered hot keys ??

BTW, the code you have runs a constant loop that won't exit until you execute the UnsetHotKey sub. If you don't do that, the application will look like frozen in the VBE (not within excel due to DoEvents).

If you use a keyboard hook, you wont experience this freezing problem but you will need to be extra careful not to have any code error out while the hook is installed otherwise you will get a nasty application crash . So you will need to have robust preventive error handling in place. Also, resetting the VBE will cause the application to crash.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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