Click here to Skip to main content
15,891,597 members
Articles / Programming Languages / C#
Article

System Hotkey Component

Rate me:
Please Sign up or sign in to vote.
4.64/5 (27 votes)
16 Oct 2002 188.4K   3.1K   71   33
handling a System-wide Hotkey

Introduction

SystemHotkey is a simple component providing a wrapper for RegisterHotkey / UnregisterHotkey Win32-Api functions. The component creates a window which listen for WM_HOTKEY messages.

This shows the code inserted by Windows Forms Designer:

C#
private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    this.systemHotkey1 = new CodeProject.SystemHotkey.SystemHotkey(
           this.components);
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // systemHotkey1
    // 
    this.systemHotkey1.Shortcut = System.Windows.Forms.Shortcut.AltF6;
    this.systemHotkey1.Pressed += new System.EventHandler(
           this.systemHotkey1_Pressed);

The Shortcut property sets the Hotkey and activates it. When the Hotkey is pressed the Pressed event is fired. The Hotkey is automaticaly unregistered at Dispose()

You can use this Component with a Tray-Application which activates when a Hotkey is pressed. The files Systemhotkey.cs and win32.cs should be included in a class-library

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicense or Terms Of Use? Pin
ChristopJB14-Oct-08 12:19
ChristopJB14-Oct-08 12:19 
GeneralDon't power off or restart the PC Pin
kriegsmar1-Jun-07 11:11
kriegsmar1-Jun-07 11:11 
QuestionVista bug? Pin
ETL .Net28-Mar-07 14:26
ETL .Net28-Mar-07 14:26 
QuestionUsing Windows as a modifier key? Pin
Kir Birger20-Oct-06 3:17
Kir Birger20-Oct-06 3:17 
GeneralPlease fix multiple Mod key selection Pin
countryBumkin15-Jun-06 9:13
countryBumkin15-Jun-06 9:13 
QuestionCan you add event KeyDown and KeyUp Please! Pin
ndphuong4-May-06 1:29
ndphuong4-May-06 1:29 
AnswerRe: Can you add event KeyDown and KeyUp Please! Pin
Kir Birger20-Oct-06 3:15
Kir Birger20-Oct-06 3:15 
Questionwhere are the numbers coming from? Pin
haztek software8-Mar-06 17:03
haztek software8-Mar-06 17:03 
AnswerRe: where are the numbers coming from? Pin
Alexander Werner8-Mar-06 22:07
Alexander Werner8-Mar-06 22:07 
GeneralBinding Hotkey in real time Pin
Markclee3-Nov-05 12:40
Markclee3-Nov-05 12:40 
GeneralRe: Binding Hotkey in real time Pin
Skoobie Du26-Jun-07 2:38
Skoobie Du26-Jun-07 2:38 
GeneralDefault functionality of mapped hotkey Pin
makasili27-Oct-05 10:45
makasili27-Oct-05 10:45 
GeneralRe: Default functionality of mapped hotkey Pin
Skoobie Du26-Jun-07 2:19
Skoobie Du26-Jun-07 2:19 
GeneralOn lost focus - Hotkeys should get unregistered Pin
sona_zar25-Aug-05 1:22
sona_zar25-Aug-05 1:22 
GeneralRe: On lost focus - Hotkeys should get unregistered Pin
Alexander Werner25-Aug-05 1:47
Alexander Werner25-Aug-05 1:47 
GeneralRe: On lost focus - Hotkeys should get unregistered Pin
sona_zar25-Aug-05 3:42
sona_zar25-Aug-05 3:42 
GeneralRe: On lost focus - Hotkeys should get unregistered Pin
sona_zar25-Aug-05 4:25
sona_zar25-Aug-05 4:25 
GeneralRe: On lost focus - Hotkeys should get unregistered Pin
Skoobie Du26-Jun-07 1:59
Skoobie Du26-Jun-07 1:59 
GeneralRe: On lost focus - Hotkeys should get unregistered Pin
ekb02116-Jul-07 10:52
ekb02116-Jul-07 10:52 
GeneralUpdate to RegisterHotKey method Pin
Rick Hobbs @ NeoWorks24-Sep-04 0:47
sussRick Hobbs @ NeoWorks24-Sep-04 0:47 
GeneralRe: Update to RegisterHotKey method Pin
timbits21410-Dec-04 19:29
timbits21410-Dec-04 19:29 
GeneralRe: Update to RegisterHotKey method Pin
timbits21411-Dec-04 12:13
timbits21411-Dec-04 12:13 
GeneralRe: Update to RegisterHotKey method Pin
Xaroth18-May-06 0:22
Xaroth18-May-06 0:22 
also a note, the 'id' field of the RegisterHotkey API call is an id which will be returned by the WndMsg if pressed, by toying with that and giving multiple hotkeys different ids you can bind more than 1 hotkey per class
Parameters

    hWnd
        [in] Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop. 
    id
        [in] Specifies the identifier of the hot key. No other hot key in the calling thread should have the same identifier. An application must specify a value in the range 0x0000 through 0xBFFF. A shared dynamic-link library (DLL) must specify a value in the range 0xC000 through 0xFFFF (the range returned by the GlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier. 
    fsModifiers
        [in] Specifies keys that must be pressed in combination with the key specified by the uVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values.

        MOD_ALT
            Either ALT key must be held down.
        MOD_CONTROL
            Either CTRL key must be held down.
        MOD_SHIFT
            Either SHIFT key must be held down.
        MOD_WIN
            Either WINDOWS key was held down. These keys are labeled with the Microsoft Windows logo.

    vk
        [in] Specifies the virtual-key code of the hot key. 

GeneralRe: Update to RegisterHotKey method Pin
countryBumkin15-Jun-06 9:11
countryBumkin15-Jun-06 9:11 
GeneralRe: Update to RegisterHotKey method Pin
olveram730-Jun-06 14:36
olveram730-Jun-06 14:36 

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

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