How do i create a hotkey?
Printable View
How do i create a hotkey?
You can use the RegisterHotKey API's, i.e.
In a Module:Example Usage:Code:Private Type POINTAPI
x As Long
y As Long
End Type
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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
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 SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WH_GETMESSAGE = 3
Private Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Private lHookID As Long
Private lHotKeys As Long
Private Function CallBackHook(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim tMSG As MSG
CopyMemory tMSG, ByVal lParam, Len(tMSG)
If tMSG.message = WM_HOTKEY And wParam Then
'Execute whatever for the HotKey here
Form1.lPressed = tMSG.wParam
Form1.Command1_Click
End If
If nCode < 0 Then CallBackHook = CallNextHookEx(lHookID, nCode, wParam, ByVal lParam)
End Function
Public Function SetHotKey(ByVal lSpecial As Long, ByVal lKey As Long) As Long
Static lHotKeyID As Long
lHotKeyID = lHotKeyID + 1
If RegisterHotKey(0&, lHotKeyID, lSpecial, lKey) <> 0 Then
lHotKeys = lHotKeys + 1
SetHotKey = lHotKeyID
If lHookID = 0 Then lHookID = SetWindowsHookEx(WH_GETMESSAGE, AddressOf CallBackHook, App.hInstance, App.ThreadID)
End If
End Function
Public Sub RemoveHotKey(ByVal lHotKeyID As Long)
If UnregisterHotKey(0&, lHotKeyID) Then
If lHotKeys > 0 Then lHotKeys = lHotKeys - 1
End If
If lHotKeys = 0 And lHookID <> 0 Then
Call UnhookWindowsHookEx(lHookID)
lHookID = 0
End If
End Sub
Code:Private lHotKey(3) As Long
Public lPressed As Long
Private Sub Form_Load()
lHotKey(0) = SetHotKey(0, vbKeyF9)
lHotKey(1) = SetHotKey(0, vbKeyF10)
lHotKey(2) = SetHotKey(0, vbKeyF11)
lHotKey(3) = SetHotKey(0, vbKeyF12)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim lIndex As Long
For lIndex = 0 To 3
RemoveHotKey lHotKey(lIndex)
Next
End Sub
Public Sub Command1_Click()
'Make the Command Buttons Click Event Public, so it can be called outside the Forms Module.
MsgBox "You pressed the Hotkey with the ID of: " & lPressed, vbSystemModal
End Sub
if thats a little too complicated for you(like it is for me)
you could use GetAsyncKeyState
I am using that to check for escape key press in my API Spy.Code:Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
Private Const VK_ESCAPE = &H1B
Private Sub tmrKey_Timer()
'check for escape-key keypress
If GetAsyncKeyState(VK_ESCAPE) = -32767 Then
tmrWnd.Enabled = False
End If
End Sub
I set the interval to 25
Thanks Aaron Young!! that did it!!
and thanks to Denniswrenn too!