Private Type POINTAPI
x As Long
y As Long
End Type
#If VBA7 Then
Private Type MSG
hwnd As LongPtr
message As Long
wParam As LongPtr
lParam As LongPtr
time As Long
pt As POINTAPI
End Type
Private Declare PtrSafe Function RegisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare PtrSafe Function UnregisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long) As Long
Private 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
Private Declare PtrSafe Function WaitMessage Lib "user32" () As Long
#Else
Private Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
#End If
Private Const MOD_ALT = &H1
Private Const NUMPAD1 = &H61
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private bCancel As Boolean
Private Sub UserForm_Activate()
[COLOR=#008000] '============================================================
'run any other pre-existing code here before hooking the keys.
'============================================================[/COLOR]
[COLOR=#008000]'Hook the Alt + 1 keys combination.[/COLOR]
bCancel = False
Call RegisterHotKey(Application.hwnd, &HBFFF&, MOD_ALT, vbKey1)
Call RegisterHotKey(Application.hwnd, &HBFFF&, MOD_ALT, NUMPAD1)
Call Key_Listener
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
bCancel = True
End Sub
Private Sub Key_Listener()
Dim message As MSG
On Error GoTo Oops
Do While Not bCancel
WaitMessage
If PeekMessage(message, Application.hwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
[COLOR=#008000]'=========================================================
'Comment out this line if you don't wish to activate excel.[/COLOR]
VBA.AppActivate Application.Caption
[COLOR=#008000] '=========================================================
'Execute the 'Test' procedure.[/COLOR]
Call Test
End If
DoEvents
Loop
Oops:
Call UnregisterHotKey(Application.hwnd, &HBFFF&)
End Sub
Private Sub Test()
MsgBox "Procedure invoked !", vbApplicationModal
End Sub