Click here to Skip to main content
Licence 
First Posted 16 Oct 2002
Views 130,934
Bookmarked 67 times

System Hotkey Component

By | 16 Oct 2002 | Article
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:

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

About the Author

Alexander Werner

Web Developer

Germany Germany

Member



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
QuestionLicense or Terms Of Use? PinmemberChristopJB12:19 14 Oct '08  
GeneralDon't power off or restart the PC Pinmemberkriegsmar11:11 1 Jun '07  
QuestionVista bug? PinmemberETL .Net14:26 28 Mar '07  
QuestionUsing Windows as a modifier key? PinmemberKbirger3:17 20 Oct '06  
GeneralPlease fix multiple Mod key selection PinmembercountryBumkin9:13 15 Jun '06  
QuestionCan you add event KeyDown and KeyUp Please! Pinmemberndphuong1:29 4 May '06  
AnswerRe: Can you add event KeyDown and KeyUp Please! PinmemberKbirger3:15 20 Oct '06  
Questionwhere are the numbers coming from? Pinmemberjhazlett17:03 8 Mar '06  
AnswerRe: where are the numbers coming from? PinmemberAlexander Werner22:07 8 Mar '06  
GeneralBinding Hotkey in real time PinmemberMarkclee12:40 3 Nov '05  
GeneralRe: Binding Hotkey in real time PinmemberSkoobie Du2:38 26 Jun '07  
GeneralDefault functionality of mapped hotkey Pinmembermakasili10:45 27 Oct '05  
GeneralRe: Default functionality of mapped hotkey PinmemberSkoobie Du2:19 26 Jun '07  
GeneralOn lost focus - Hotkeys should get unregistered Pinmembersona_zar1:22 25 Aug '05  
GeneralRe: On lost focus - Hotkeys should get unregistered PinmemberAlexander Werner1:47 25 Aug '05  
GeneralRe: On lost focus - Hotkeys should get unregistered Pinmembersona_zar3:42 25 Aug '05  
GeneralRe: On lost focus - Hotkeys should get unregistered Pinmembersona_zar4:25 25 Aug '05  
GeneralRe: On lost focus - Hotkeys should get unregistered PinmemberSkoobie Du1:59 26 Jun '07  
GeneralRe: On lost focus - Hotkeys should get unregistered Pinmemberekb021110:52 6 Jul '07  
GeneralUpdate to RegisterHotKey method PinsussRick Hobbs @ NeoWorks0:47 24 Sep '04  
GeneralRe: Update to RegisterHotKey method Pinmembertimbits21419:29 10 Dec '04  
GeneralRe: Update to RegisterHotKey method Pinmembertimbits21412:13 11 Dec '04  
GeneralRe: Update to RegisterHotKey method PinmemberXaroth0:22 18 May '06  
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 PinmembercountryBumkin9:11 15 Jun '06  
GeneralRe: Update to RegisterHotKey method Pinmemberolveram714:36 30 Jun '06  

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.120529.1 | Last Updated 17 Oct 2002
Article Copyright 2002 by Alexander Werner
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid