RegisterHotKey

By | 2002-06-01

Register a system wide hot key

Original Author: Andy Barilla

API Declarations

Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
Const MOD_CONTROL = &H2
Const MOD_SHIFT = &H4
Const MOD_WIN = &H8
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 GlobalAddAtom Lib “kernel32” Alias “GlobalAddAtomA” (ByVal lpString As String) As Integer
Private Declare Function GlobalDeleteAtom Lib “kernel32” (ByVal nAtom As Integer) As Integer
Private Declare Function UnregisterHotKey Lib “user32” (ByVal hwnd As Long, ByVal id As Long) As Long

Code

'This example uses the MsgHook OCX but any similar OCX would also work
'iAtom stores the id used by the hotkey. If you have more then one hot key, use one atom for each
Dim iAtom As Integer
Private Sub Form_Load()
  Dim res As Long
  'Get a value for atom  
  iAtom = GlobalAddAtom("MyHotKey")
  'Register the Ctrl-Alt-T key combination as the hotkey
  res = RegisterHotKey(Me.hwnd, iAtom, MOD_ALT + MOD_CTRL, vbKeyT)
  'Setup msghook to receive the WM_HOTKEY message  
  Msghook1.HwndHook = Me.hwnd
  Msghook1.Message(WM_HOTKEY) = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
  Dim res As Long
  'Remove the hotkey and delete the atom
  res = UnregisterHotKey(Me.hwnd, iAtom)
  res = GlobalDeleteAtom(iAtom)
End Sub
Private Sub Msghook1_Message(ByVal msg As Long, ByVal wp As Long, ByVal lp As Long, result As Long)
  If msg = WM_HOTKEY Then
    If wp = iAtom Then
      'Do your thang...
      MsgBox "Boing!!!"
    End If
  End If
  Msghook1.InvokeWindowProc msg, wp, lp
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.