C# 系統(tǒng)熱鍵注冊實現(xiàn)代碼
更新時間:2009年02月14日 14:20:50 作者:
簡單點說就是為程序制定快捷鍵勒。。很多軟件都帶熱鍵功能的,通過以下方式可以實現(xiàn)2個鍵或3個鍵的快捷鍵,相當(dāng)之使用,具體實現(xiàn)方法看后文吧。
先引用using System.Runtime.InteropServices; 的命名空間,
然后在合適的位置加上如下代碼就OK。。注意:Form1_Load和Form1_FormClosed不能直接copy哦~
[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);
}
輔助鍵說明:
None = 0,
Alt = 1,
crtl= 2,
Shift = 4,
Windows = 8
如果有多個輔助鍵則,例如 alt+crtl是3 直接相加就可以了
然后在合適的位置加上如下代碼就OK。。注意:Form1_Load和Form1_FormClosed不能直接copy哦~
復(fù)制代碼 代碼如下:
[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);
}
輔助鍵說明:
None = 0,
Alt = 1,
crtl= 2,
Shift = 4,
Windows = 8
如果有多個輔助鍵則,例如 alt+crtl是3 直接相加就可以了
相關(guān)文章
Unity編輯器資源導(dǎo)入處理函數(shù)OnPreprocessAudio用法示例
這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPreprocessAudio用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08