Click here to Skip to main content
11,705,762 members (50,365 online)
Click here to Skip to main content

.NET system wide hotkey component

, 18 Apr 2006 CPOL 199.6K 1K 104
Rate this:
Please Sign up or sign in to vote.
A component to respond to a hotkey combination, system-wide.

Introduction

A system wide hotkey is a key combination that fires a specific event regardless of which application has the input focus. For example, if you press WIN+E, then a new instance of Explorer is fired up, or if you press CTRL+PRINTSCREEN the currently active window is screen printed to the clipboard.

The Win32 API has a couple of calls which are used to set up and respond to hotkeys - specifically RegisterHotKey and UnregisterHotKey which you declare in VB.NET thus:

<DllImport("user32", EntryPoint:="RegisterHotKey", _
          SetLastError:=True, _
          ExactSpelling:=True, _
          CallingConvention:=CallingConvention.StdCall)> _
Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
           ByVal Id As Int32, _
           <MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
           <MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean

End Function

<DllImport("user32", EntryPoint:="UnregisterHotKey", _
SetLastError:=True, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function UnregisterHotkey(ByVal hwnd As Int32, _
                            ByVal Id As Int32) As Boolean

End Function

After a hotkey has been registered by the API call RegisterHotkey, then whenever the key combination specified is pressed, a WM_HOTKEY Windows message is sent to the window passed to it in the hwnd parameter.

Specifying the key combination

The key that triggers the event is defined by the two parameters fsModifiers and vKey.

fsModifiers is a value which tells the operating system, which modifier keys are included in the hotkey combination - i.e. Alt, Ctrl, Shift or Win key. This value is made by a combination of the following flags:

Public Enum HotkeyModifierFlags
    MOD_ALT = &H1
    MOD_CONTROL = &H2
    MOD_SHIFT = &H4
    MOD_WIN = &H8
End Enum

Thus, for example, if you want a hotkey which is CTRL+ALT+C, then the modifier would be MOD_ALT + MOD_CONTROL.

The vKey parameter is the virtual key code of the key that goes along with the modifier to define the hot key combination.

Specifying a unique hotkey ID

The id parameter is used to differentiate between multiple hotkeys that can be received by a given window. To ensure that we are dealing with an unique ID, I use the API call GlobalAddAtom to return the id. This is declared thus:

<DllImport("kernel32", EntryPoint:="GlobalAddAtom", _
SetLastError:=True, _
ExactSpelling:=False)> _
Public Function GlobalAddAtom(<MarshalAs(UnmanagedType.LPTStr)> _
                ByVal lpString As String) As Int32

End Function

Creating a window to listen for the WM_HOTKEY message

The final parameter is a window handle to listen for the WM_HOTKEY message. Here there are two options - either pass in the handle of an existing window and subclass it's WndProc to look out for the message or create a new invisible window specifically to listen for the message.

The second option is achieved by creating a new class that derives from System.Windows.Forms.NativeWindow thus:

Public Class GlobalHotkeyListener
    Inherits NativeWindow

#Region "Private member variables"
    Private windowHandle As Integer
    Private mwh As ManualResetEvent
#End Region

    Public Sub New(ByVal Id As Int32, _
              ByVal fsModifiers As Int32, _
              ByVal vkey As Int32, _
              ByRef wh As ManualResetEvent)

        '\\ Get a local copy of the wait handle

        mwh = wh
        Dim cp As CreateParams = New CreateParams()

        ' Fill in the CreateParams details.
        cp.Caption = ""
        cp.ClassName = "STATIC"

        ' Set the position on the form
        cp.X = 0
        cp.Y = 0
        cp.Height = 0
        cp.Width = 0

        '\\ Set the style and extended style flags
        cp.Style = WindowStyleBits.WS_MINIMIZE
        cp.ExStyle = WindowStyleExtendedBits.WS_EX_NOACTIVATE

        ' Create the actual window
        Me.CreateHandle(cp)

        Try
            If Not RegisterHotkey(MyBase.Handle, _
                        Id, fsModifiers, vkey) Then
                Throw New Win32Exception()
            End If
        Catch e As Exception
            System.Diagnostics.Debug.WriteLine(e.ToString)
        End Try
    End Sub

And the listening for the WM_HOTKEY is done thus:

<System.Security.Permissions._
                 PermissionSetAttribute(System.Security.Permissions_
                 .SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As Message)
        ' Listen for messages that are sent to the 
        ' button window. Some messages are sent
        ' to the parent window instead of the button's window.

        Select Case (m.Msg)
            Case WM_HOTKEY
                ' Respond to the hotkey message (asynchronously??)
                If Not mwh Is Nothing Then
                    mwh.Set()
                End If
        End Select

        MyBase.WndProc(m)
    End Sub

Future improvements

Ideally this control should have a UIEditor derived class to allow the selection of the key combination.

License

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

Share

About the Author

Duncan Edwards Jones
Software Developer (Senior)
Ireland Ireland
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET

You may also be interested in...

Comments and Discussions

 
QuestionWin64 hotkey register Pin
pgandhi7-Oct-10 22:33
memberpgandhi7-Oct-10 22:33 
GeneralI'm very thankful Pin
Phani Pradeep16-Mar-09 4:39
memberPhani Pradeep16-Mar-09 4:39 
QuestionPrintScreen Pin
mahone8-Jan-09 6:46
membermahone8-Jan-09 6:46 
GeneralWhere do I declare the hotkey combination Pin
isgrom29-Oct-07 4:21
memberisgrom29-Oct-07 4:21 
Newsyou can instead it with this Pin
Wan--Vevi31-Aug-07 15:39
memberWan--Vevi31-Aug-07 15:39 
GeneralRe: you can instead it with this Pin
Duncan Edwards Jones1-Sep-07 10:16
memberDuncan Edwards Jones1-Sep-07 10:16 
QuestionMultiple hotkeys Pin
stupid481-Jun-07 8:11
memberstupid481-Jun-07 8:11 
AnswerRe: Multiple hotkeys Pin
mahone13-Jan-09 10:50
membermahone13-Jan-09 10:50 
AnswerRe: Multiple hotkeys Pin
Member 1179416725-Jun-15 22:57
memberMember 1179416725-Jun-15 22:57 
QuestionDouble Tab Hotkey Pin
HakunaMatada16-May-07 2:00
memberHakunaMatada16-May-07 2:00 
GeneralMCLHotkey.xml Pin
moa7718-Jan-07 22:04
membermoa7718-Jan-07 22:04 
QuestionHotkey as Windows service Pin
moa7714-Dec-06 8:21
membermoa7714-Dec-06 8:21 
AnswerRe: Hotkey as Windows service Pin
Duncan Edwards Jones14-Dec-06 12:08
memberDuncan Edwards Jones14-Dec-06 12:08 
QuestionRe: Hotkey as Windows service Pin
moa7714-Dec-06 22:20
membermoa7714-Dec-06 22:20 
GeneralRe: Hotkey as Windows service Pin
moa7717-Dec-06 10:51
membermoa7717-Dec-06 10:51 
GeneralRe: Hotkey as Windows service Pin
kraghavk23-Jul-07 22:00
memberkraghavk23-Jul-07 22:00 
GeneralBuild HotKeyTest for install Pin
moa7718-Jul-06 0:30
membermoa7718-Jul-06 0:30 
GeneralRe: Build HotKeyTest for install Pin
Duncan Edwards Jones18-Jul-06 1:02
memberDuncan Edwards Jones18-Jul-06 1:02 
GeneralRe: Build HotKeyTest for install Pin
moa7718-Jul-06 1:45
membermoa7718-Jul-06 1:45 
GeneralRe: Build HotKeyTest for install Pin
Duncan Edwards Jones18-Jul-06 2:49
memberDuncan Edwards Jones18-Jul-06 2:49 
GeneralRe: Build HotKeyTest for install Pin
moa7718-Jul-06 2:59
membermoa7718-Jul-06 2:59 
QuestionHow to Use it? Pin
Barrin8613-Jun-06 21:46
memberBarrin8613-Jun-06 21:46 
GeneralMissing Test App Pin
J Benjamin29-Aug-05 8:22
memberJ Benjamin29-Aug-05 8:22 
GeneralRe: Missing Test App Pin
Duncan Edwards Jones19-Sep-05 0:24
memberDuncan Edwards Jones19-Sep-05 0:24 
GeneralTest Application Missing Pin
Mayank Gupta31-Mar-05 14:58
memberMayank Gupta31-Mar-05 14:58 
GeneralRe: Test Application Missing Pin
jahooper20-Apr-05 10:03
memberjahooper20-Apr-05 10:03 
GeneralRe: Test Application Missing Pin
bayter5-Jul-05 10:49
memberbayter5-Jul-05 10:49 
GeneralRe: Test Application Missing Pin
Duncan Edwards Jones19-Sep-05 0:31
memberDuncan Edwards Jones19-Sep-05 0:31 
GeneralRe: Test Application Missing Pin
eternalsoul16-Sep-05 20:05
membereternalsoul16-Sep-05 20:05 
GeneralRe: Test Application Missing Pin
Duncan Edwards Jones19-Sep-05 0:30
memberDuncan Edwards Jones19-Sep-05 0:30 
Generaldoesn't work if form.showintaskbar = false Pin
kraquen15-Feb-05 6:16
memberkraquen15-Feb-05 6:16 
QuestionHotkeyPressed event? Pin
Jehosephat24-Nov-04 3:32
memberJehosephat24-Nov-04 3:32 
AnswerRe: HotkeyPressed event? Pin
Merrion24-Nov-04 4:28
memberMerrion24-Nov-04 4:28 
GeneralRe: HotkeyPressed event? Pin
Jehosephat24-Nov-04 7:41
memberJehosephat24-Nov-04 7:41 
GeneralMultiple hotkeys... Pin
dzCepheus11-Dec-03 7:38
memberdzCepheus11-Dec-03 7:38 
GeneralRe: Multiple hotkeys... Pin
dzCepheus11-Dec-03 7:43
memberdzCepheus11-Dec-03 7:43 
GeneralRe: Multiple hotkeys... Pin
Merrion11-Dec-03 8:20
memberMerrion11-Dec-03 8:20 
GeneralRe: Multiple hotkeys... Pin
dzCepheus11-Dec-03 9:25
memberdzCepheus11-Dec-03 9:25 
GeneralRe: Multiple hotkeys... Pin
Merrion11-Dec-03 22:17
memberMerrion11-Dec-03 22:17 
GeneralRe: Multiple hotkeys... Pin
dzCepheus11-Dec-03 22:25
memberdzCepheus11-Dec-03 22:25 
GeneralRe: Multiple hotkeys... Pin
gregory_may@hotmail.com28-Apr-06 11:35
membergregory_may@hotmail.com28-Apr-06 11:35 
GeneralRe: Multiple hotkeys... Pin
Barrin8614-Jun-06 0:10
memberBarrin8614-Jun-06 0:10 
GeneralRe: Multiple hotkeys... Pin
MrWhite34026-Aug-06 23:27
memberMrWhite34026-Aug-06 23:27 
GeneralRe: Multiple hotkeys... Pin
ketiko29-Jan-07 13:12
memberketiko29-Jan-07 13:12 
GeneralRe: Multiple hotkeys... Pin
Duncan Edwards Jones29-Jan-07 20:42
memberDuncan Edwards Jones29-Jan-07 20:42 
QuestionRe: Multiple hotkeys... [modified] Pin
LethPhaos23-May-07 10:43
memberLethPhaos23-May-07 10:43 
QuestionDefects or suggestions? Pin
Merrion16-Jun-03 3:57
memberMerrion16-Jun-03 3:57 
AnswerRe: Defects or suggestions? Pin
Uwe Keim16-Jun-03 7:09
sitebuilderUwe Keim16-Jun-03 7:09 
AnswerRe: Defects or suggestions? Pin
Blake Coverett16-Jun-03 17:42
memberBlake Coverett16-Jun-03 17:42 
AnswerRe: Defects or suggestions? Pin
JonathanSeattle16-Apr-06 0:34
memberJonathanSeattle16-Apr-06 0:34 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 18 Apr 2006
Article Copyright 2003 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid