例如:
if(e.KeyValue==(char)Keys.W && e.Control )//Ctrl+W
{
ToolBarButtonClickEventArgs ee=new ToolBarButtonClickEventArgs(this.toolBarButtonWorkAttend);
toolBarNavigation_ButtonClick(this.toolBarButtonWorkAttend,ee);
}
if(e.KeyValue==(char)Keys.W && e.Control )//Ctrl+W
{
ToolBarButtonClickEventArgs ee=new ToolBarButtonClickEventArgs(this.toolBarButtonWorkAttend);
toolBarNavigation_ButtonClick(this.toolBarButtonWorkAttend,ee);
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace HotKey
{
public class Form1 : System.Windows.Forms.Form
{
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.HighlightText;
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(272, 144);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Error Message:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 191);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new Form1());
} public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool
bWindows,Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
modifier |= WinHotKey.KeyModifiers.Control;
if(this.key_Alt )
modifier |= WinHotKey.KeyModifiers.Alt;
if(this.key_Shift)
modifier |= WinHotKey.KeyModifiers.Shift;
if(this.key_Windows)
modifier |= WinHotKey.KeyModifiers.Windows;
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
}
catch(Exception err)
{
this.textBox1.AppendText(err.Message + "\r\n");
}
} protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息
this.textBox1.AppendText(m.Msg.ToString() + "\r\n");
break;
}
}
base.WndProc(ref m );
} private void Form1_Load(object sender, System.EventArgs e)
{
this.SetHotKey(true, false, false, false, Keys.F10);
}
}
#region WinHotKey Class
public class WinHotKey
{
[DllImport("user32.dll",SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
int id,
KeyModifiers fsModifiers,
Keys vk
); [DllImport("user32.dll",SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control =2,
Shift = 4,
Windows = 8
} public WinHotKey(){}
}
#endregion
}