Click here to Skip to main content
15,880,392 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 187.5K   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 
In short, no. That data was never copied, so it couldn't get lost in the first place.

Hotkeys like Ctrl+C (Copy), Ctrl+V (Paste), Ctrl+X (Cut), Ctrl+A (Select All), etc. are standardized hotkeys, but their functionality are decided by the programmer(s). I could use Ctrl+C to close my application if I wanted to. So, to repeat, those hotkeys are not system-wide hotkeys (not defined by the system).

System-wide hotkeys include Ctrl+Print Screen (Copy Screen Capture to the Clipboard), Alt+Tab (Rotate Forward Through Open Windows), Win+R (Open Run Dialog), Print Screen (Screen Capture), Alt+Shift+Tab (Rotate Backward Through Open Windows), etc. These are registered by Windows, and cannot be used by any application (that I know of).

So, to do what you're wanting to do, you need to access the functions/methods that copy the data to the clipboard while in that application. The application that is intercepting the system-wide hotkey(s) is grabbing the messages before they get the data-holding application, so the data-holding application doesn't know that you want to copy the intended data. You would need to have the intercepting app "connect" to the data-holding app, and use the functions/methods it has defined to copy the intended data. Good luck.

---------------------------------------------------------------------------
Skoobie Du, programming and web developing since I was 12. ^_^ I might not be very good, but I don't care what you think. Sleepy | :zzz: Suspicious | :suss: Cool | :cool:
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 
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.