using System.Runtime.InteropServices;
//注册热键的api
[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);
RegisterHotKey(this.Handle,888,0,Keys.A);//这句放在LOAD事件里面
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
if(m.WParam.ToString().Equals("888")) //这是我们注册的那個热键
MessageBox.Show("我按了BUTTON键");//这里可以改改,比如button1_Click(this,EventArgs.Empty);
break;
}
base.WndProc (ref m);
}
C# 捕获键盘按键的消息
最新推荐文章于 2024-08-12 08:51:49 发布