C# 系統熱鍵註冊實現代碼

來源:互聯網
上載者:User

先引用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 直接相加就可以了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.