Click here to Skip to main content
Licence CPOL
First Posted 12 May 2003
Views 61,226
Downloads 960
Bookmarked 17 times

MCL Global Hotkey ActiveX control

By | 12 May 2003 | Article
A system wide hotkey control written in VB6

Introduction

The attached source code is for a system-wide global hotkey control written in Visual Basic 6, originally posted on the Merrion Computing Downloads.

Registering a hotkey

There are two API calls concerned with registering a system wide hotkey: RegisterHotkey and UnregisterHotkey - the declarations are:

To register the hotkey, you need to specify hwnd - the window which will be notified when the hotkey combination is pressed, id - the unique identifier for the hotkey, fsModifiers - the modifiers of the hotkey (i.e. whether ALT, SHIFT, CTRL or WIN keys are pressed) and finally vKey - the virtual key code for the hot key. The modifiers which can be combined together are:

Listening Out for the hotkey

When a hotkey which has been registered successfully is pressed, the window specified in the hwnd is posted a WM_HOTKEY registered window message. Additionally the lParam parameter holds the modifier and virtual key code in its high and low words respectively.

Visual Basic does not handle the WM_HOTKEY message by default which means that you need to subclass the window hwnd in order to respond to it. To do this, you use the API call SetWindowLong with the index GWL_WNDPROC to replace the existing window procedure with yours.

Using the Control in your Application

To use this control in a Visual Basic application, compile the attached source code and then add an instance of the resulting OCX to your project. Add an instance of the MCLHotkey control to your form and press F4 to set the properties. Then add the code that you want executed when the hotkey is pressed to the HotkeyPressed event:

Private Sub MCLHotkey1_HotkeyPressed()
  Debug.Print "You pressed the hotkey at " & Now
End Sub
'\\ API Calls used in subclassing windows....
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 DefWindowProc Lib "user32" Alias "DefWindowProcA" _
	(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
	ByVal lParam As Long) As Long
'\\ Window specific information
Private Declare Function GetWindowLongApi Lib "user32" Alias "GetWindowLongA" _
	(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongApi Lib "user32" Alias "SetWindowLongA" _
	(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)
Public Function VB_WindowProc(ByVal hwnd As Long, ByVal wMsg As Long, _
	ByVal wParam As Long, ByVal lParam As Long) As Long

Dim VKey As Long
Dim Modifier As Long

If wMsg = WM_HOTKEY Then
    VKey = HiWord(lParam)
    Modifier = LoWord(lParam)
    '\\ Raise an event here to respond to the hotkey
End If

If sWindow.OldWndProc = 0 Then
    VB_WindowProc = DefWindowProc(hwnd, wMsg, wParam, lParam)
Else
    VB_WindowProc = CallWindowProc(sWindow.OldWndProc, hwnd, wMsg, wParam, lParam)
End If

End Function
Public Enum enHotkeyModifiers
    MOD_ALT = &H1
    MOD_CONTROL = &H2
    MOD_SHIFT = &H4
    MOD_WIN = &H8
End Enum
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

History

  • 12th May, 2003: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duncan Edwards Jones

Software Developer (Senior)
JP Morgan
Ireland Ireland

Member

Follow on Twitter Follow on Twitter
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalmisbehave on window vista Pinmemberarunkumar.1658322:19 30 Jan '09  
Generalthis is great but PinmemberLeerz6:16 5 Dec '06  
GeneralRe: this is great but PinmemberDuncan Edwards Jones3:51 8 Feb '07  
GeneralExcellent Control but a couple of problems PinmemberAvoSoftwareTeam2:38 29 Oct '05  
GeneralRe: Excellent Control but a couple of problems PinmemberDuncan Edwards Jones6:32 29 Oct '05  
GeneralDisable the [Ctrl]+[Esc] keys PinsussAnonymous11:46 13 Jan '05  
GeneralGreat but... PinmemberQ_HAL4:36 2 Aug '04  
GeneralNice Ariticle:reason why I rated this excellent PinmemberIpog Gates0:45 18 Jan '04  
General.NET Version PinmemberMerrion0:45 13 Jun '03  
GeneralRe: .NET Version PinmemberJehosephat10:34 22 Nov '04  
GeneralRe: .NET Version PinmemberMerrion12:14 22 Nov '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 13 May 2003
Article Copyright 2003 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid