[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 Form2_Load1(object sender, System.EventArgs e)
{
RegisterHotKey(this.Handle,888,2,Keys.A);
//handle:這個窗體的handle 888:這個熱鍵的標誌id 2:crtl鍵 A: a鍵
}
private void Form2_Load1(object sender, System.EventArgs e)
{
UnregisterHotKey(this.Handle,888);
//handle:這個窗體的handle 888:上面那個熱鍵的標誌id
}
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);
}
None = 0,
Alt = 1,
crtl= 2,
Shift = 4,
Windows = 8
這是熱鍵的定義 alt+crtl是3 直接相加就可以了
如果用sendmessagea這個api 就是
SendMessage(this.Handle,WM_SETHOTKEY,0x45a, 0)
//WM_SETHOTKEY 是 0x32
//0x45a 高字節是contrl鍵 低字節是a b c d 所以5a是z的ascii 4是alt 也就是alt+z