65.9K
CodeProject is changing. Read more.
Home

System Hotkey Component

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (27 votes)

Oct 17, 2002

viewsIcon

191840

downloadIcon

3103

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