Search Tools Links Login


Posted: 2002-06-01
By: ArchiveBot
Viewed: 96

Filed Under:

VB6 Code Cache

No attachments for this post

Register a system wide hot key

Original Author: Andy Barilla

API Declarations

Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
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


'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

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.