Class Hotkey.cs
Using System;
Namespace Durius.generics
{
public delegate void Hotkeyeventhandler (int hotkeyid);
<summary>
System Wide Hotkey Wrapper.
///
Robert Jeppesen
Send Bugs to Robert@durius.com
</summary>
public class Hotkey:System.Windows.Forms.IMessageFilter
{
System.Collections.Hashtable keyids = new System.Collections.Hashtable ();
IntPtr hWnd;
<summary>
Occurs when a hotkey has been pressed.
</summary>
public event Hotkeyeventhandler Onhotkey;
public enum Keyflags
{
Mod_alt = 0x1,
Mod_control = 0x2,
Mod_shift = 0x4,
Mod_win = 0x8
}
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern UInt32 RegisterHotKey (IntPtr hWnd, UInt32 ID,
UInt32 fsmodifiers, UInt32 VK);
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern UInt32 Unregisterhotkey (IntPtr hWnd, UInt32 ID);
[System.Runtime.InteropServices.DllImport ("kernel32.dll")]
public static extern UInt32 Globaladdatom (String lpstring);
[System.Runtime.InteropServices.DllImport ("kernel32.dll")]
public static extern UInt32 Globaldeleteatom (UInt32 natom);
<summary>
constructor. Adds this instance to the "Messagefilters so" this class can raise Hotkey events
</summary>
<param name= "hwnd" >a valid hwnd, i.e. Form1. Handle</param>
Public Hotkey (IntPtr hWnd)
{
This.hwnd = hWnd;
System.Windows.Forms.Application.AddMessageFilter (this);
}
<summary>
Register a system wide hotkey.
</summary>
<param name= "HWnd" >form1. Handle</param>
<param name= "Key" >your hotkey</param>
<returns>id integer for your hotkey. Use the to know which hotkey is pressed.</returns>
public int RegisterHotKey (System.Windows.Forms.Keys Key, Keyflags keyflags)
{
UInt32 Hotkeyid = Globaladdatom (System.Guid.NewGuid (). ToString ());
RegisterHotKey ((INTPTR) hWnd, Hotkeyid, (UInt32) Keyflags, (UInt32) Key);
Keyids.add (Hotkeyid, Hotkeyid);
return (int) Hotkeyid;
}
<summary>
Unregister hotkeys and delete atoms.
</summary>
public void Unregisterhotkeys ()
{
System.Windows.Forms.Application.RemoveMessageFilter (this);
foreach (UInt32 key in Keyids.values)
{
Unregisterhotkey (HWnd, key);
Globaldeleteatom (key);
}
}
public bool Prefiltermessage (ref System.Windows.Forms.Message m)
{
if (m.msg = = 0x312)/*wm_hotkey*/
{
if (Onhotkey!= null)
{
foreach (UInt32 key in Keyids.values)
{
if ((UInt32) M.wparam = = key)
{
Onhotkey ((int) m.wparam);
return true;
}
}
}
}
return false;
}
}
}
Test program Form1.cs
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using Durius.generics;
Namespace Testgenerics
{
<summary>
Summary description for Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Hotkey Hotkey;
int Hotkey1;
int Hotkey2;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.Label Label2;
Private System.Windows.Forms.Label label3;
Private System.Windows.Forms.Button button1;
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();
/*
* Initialize our hotkeys. Pass in a handle to the main form in the constructor,
* Then call RegisterHotKey for each of my hotkey combinations and wire up
* The Event
*/
Hotkey = new Hotkey (this. Handle);
Hotkey1 = Hotkey. RegisterHotKey (System.Windows.Forms.Keys.D1, Hotkey.KeyFlags.MOD_WIN);
Hotkey2 = Hotkey. RegisterHotKey (System.Windows.Forms.Keys.D2, Hotkey.KeyFlags.MOD_CONTROL);
Hotkey. Onhotkey + = new Hotkeyeventhandler (Onhotkey);
}
<summary>
The Hotkey event handler. If you are have several hotkeys, you'll have to check
Which one was pressed using Hotkeyid.
RegisterHotKey returns the Hotkeyid that is assigned to your hotkey.
</summary>
<param name= "Hotkeyid" ></param>
public void Onhotkey (int hotkeyid)
{
This. Activate ();
if (Hotkeyid = = Hotkey1)
{
MessageBox.Show ("win+1 pressed.");
}
else if (Hotkeyid = = Hotkey2)
{
MessageBox.Show ("ctrl+2 pressed.");
}
}
<summary>
Clean up any being used.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
Hotkey. Unregisterhotkeys ();
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
#region Windows Form Designer generated code
<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This.label1 = new System.Windows.Forms.Label ();
This.label2 = new System.Windows.Forms.Label ();
This.label3 = new System.Windows.Forms.Label ();
This.button1 = new System.Windows.Forms.Button ();
This. SuspendLayout ();
//
Label1
//
This.label1.Location = new System.Drawing.Point (16, 8);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (200, 16);
This.label1.TabIndex = 0;
This.label1.Text = "hotkeys:";
//
Label2
//
This.label2.Location = new System.Drawing.Point (16, 32);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (208, 16);
This.label2.TabIndex = 1;
This.label2.Text = "Win + 1";
//
Label3
//
This.label3.Location = new System.Drawing.Point (16, 56);
This.label3.Name = "Label3";
This.label3.Size = new System.Drawing.Size (208, 16);
This.label3.TabIndex = 2;
This.label3.Text = "Ctrl + 2";
//
Button1
//
This.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
This.button1.Location = new System.Drawing.Point (88, 80);
This.button1.Name = "Button1";
This.button1.TabIndex = 3;
This.button1.Text = "about";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
This. ClientSize = new System.Drawing.Size (168, 123);
This. Controls.AddRange (new system.windows.forms.control[] {
This.button1,
THIS.LABEL3,
This.label2,
This.label1});
This. Name = "Form1";
This. Text = "Hotkey";
This. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Button1_Click (object sender, System.EventArgs e)
{
MessageBox.Show (This, @ "System wide Hotkey Wrapper
-Robert Jeppesen
Http://www.durius.com "," Hotkey sample ");
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.