System Wide Hotkeys

Posted on 2003-03-03
Medium Priority
Last Modified: 2010-04-07
Can anyone please provide/point out some code which allows a user to set up hotkeys (in my application, I'd like the user to be able to assign 3-4 hotkeys to turn timers on and off, generate reports etc)

Preferably code which has actually worked for you!

I've tried some code I found on the 'Net, and even an 'MCL' component, but they didn't work out...

Question by:DoctorNash
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Author Comment

ID: 8061204
Could you please check and advise if the following hotkey code works for you:

Thanks in advance

---Form 1---

Private Sub Form_Load()
  Call SetHotKey(Me.hwnd, &H1 + &H2, &H77)
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Call RemoveHotKey
End Sub

Private Sub Command1_Click()
  App.TaskVisible = False
End Sub

---end Form 1---

---Module 1---

Option Explicit

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 CallWindowProc Lib "user32" Alias _
        "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _
        hWnd As Long, ByVal Msg As Long, ByVal wParam As _
        Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
        "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _
        As Long, ByVal dwNewLong As Long) As Long

Private Declare Function GlobalDeleteAtom Lib "kernel32" _
        (ByVal nAtom As Integer) As Integer
Private Declare Function GlobalAddAtom Lib "kernel32" Alias _
        "GlobalAddAtomA" (ByVal lpString As String) As Integer

Const GWL_WNDPROC = (-4)
Const WM_HOTKEY = &H312

Public Enum ModKeys
  MOD_ALT = &H1
  MOD_WIN = &H8
End Enum

Dim OldProc As Long, hOwner As Long
Dim iAtom As Integer

Public Sub SetHotKey(hWnd As Long, ModKey As ModKeys, _
                          vKey As Long)
  hOwner = hWnd
  iAtom = GlobalAddAtom("MyHotKey")
  Call RegisterHotKey(hOwner, iAtom, ModKey, vKey)
  OldProc = SetWindowLong(hOwner, GWL_WNDPROC, _
                          AddressOf WndProc)
End Sub

Public Sub RemoveHotKey()
  If hOwner = 0 Then Exit Sub
  Call UnregisterHotKey(hOwner, iAtom)
  Call SetWindowLong(hOwner, GWL_WNDPROC, OldProc)
  Call GlobalDeleteAtom(iAtom)
End Sub

Public Function WndProc(ByVal hWnd As Long, _
                        ByVal wMsg As Long, _
                        ByVal wParam As Long, _
                        ByVal lParam As Long) As Long
  If wMsg = WM_HOTKEY And wParam = iAtom Then
    WndProc = CallWindowProc(OldProc, hWnd, wMsg, _
                             wParam, lParam)
  End If
End Function

---End Module 1---

Accepted Solution

Naveen Swamy earned 100 total points
ID: 8062317

Expert Comment

by:Naveen Swamy
ID: 8062329

Author Comment

ID: 8069029
Navstar16, you're magnificent! The second 'codeguru' link is one that I hadn't discovered to date, and turns out to be the only code that actually works for me! A thousand thanks. I've since modified it/built on it to do what I require.

BTW, I've posted another HotKey related question:
'Re. Hotkey Creation - Simple Question but Urgent Answer needed' that you may want to take a peek at...

Author Comment

ID: 8149155

Have set up my user defined hotkeys. It works, but am getting strange behaviour with the hotkey control in the following case:

If user selects Cntrl + a key as the hotkey, then the hotkey is correctly registered ie, when user presses Cntrl + a key, the hotkey is triggered. HOWEVER, if user selects Alt + a key, or Shift + a key, the reciprocal hotkey to the one expected gets registered.

ie user thinks he has registered Alt + a key, but hotkey actually triggers when user presses Shift + a key, NOT Alt + a key.

I believe I have correctly set up the constants as
Cntrl = 2
Shift = 4
Alt = 1


Similarly, user thinks he has registered Shift + a key, but hotkey actually triggers when user presses Alt + a key NOT Shift + a key.

Why is the recognition of Alt and Shift being reversed by the system?? What's going on here?

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question