引用using System.Runtime.InteropServices; 的命名空间
[DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd,int id,uint control,Keys vk );
//注册热键的api
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private void Form1_Load(object sender, EventArgs e)
{
//注册热键(窗体句柄,热键ID,辅助键,实键)
RegisterHotKey(this.Handle, 888, 2, Keys.A);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//注消热键(句柄,热键ID)
UnregisterHotKey(this.Handle, 888);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
if (m.WParam.ToString().Equals("888")) //如果是我们注册的那个热键
MessageBox.Show("你按了ctrl+a");
break;
}
base.WndProc(ref m);
}
[DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd,int id,uint control,Keys vk );
//注册热键的api
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private void Form1_Load(object sender, EventArgs e)
{
//注册热键(窗体句柄,热键ID,辅助键,实键)
RegisterHotKey(this.Handle, 888, 2, Keys.A);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//注消热键(句柄,热键ID)
UnregisterHotKey(this.Handle, 888);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
if (m.WParam.ToString().Equals("888")) //如果是我们注册的那个热键
MessageBox.Show("你按了ctrl+a");
break;
}
base.WndProc(ref m);
}
关键字词: