c#热键

C#热键

文章分类:C++编程

为自己的程序做热键呢(快捷键)比如按“Ctrl+A”会触发自己程序的某个事件呢?

 用:

C#代码
  1. private void Form1_KeyDown(object sender, KeyEventArgs e)      
  2. {      
  3.     if (e.Control && e.KeyCode == Keys.A)      
  4.     {      
  5.         btnTest_Click(this, EventArgs.Empty);      
  6.     }      
  7. }    

 

 

方法固然简单有效!但是有没有发现一个问题?那就是当程序失去焦点的时候这个热键(快捷键)就不管用了!那怎么办呢?其实办法还

是有的!又要用到Win32API了这次用:RegisterHotKey 和 UnregisterHotKey这2个函数的意思不需要我解释吧! 看表面意思

就知道了 注册热键和注销热键我们看看他们的生命方式

 

C#代码
  1. [DllImportAttribute("user32.dll", EntryPoint = "RegisterHotKey")]      
  2.         public static extern bool RegisterHotKey      
  3.             (      
  4.                 IntPtr hWnd,        //要注册热键的窗口句柄      
  5.                 int id,             //热键编号      
  6.                 int fsModifiers,    //特殊键如:Ctrl,Alt,Shift,Window      
  7.                 int vk              //一般键如:A B C F1,F2 等      
  8.             );      
  9.      
  10.      
  11.         [DllImportAttribute("user32.dll", EntryPoint = "UnregisterHotKey")]      
  12.         public static extern bool UnregisterHotKey      
  13.             (      
  14.                 IntPtr hWnd,        //注册热键的窗口句柄      
  15.                 int id              //热键编号上面注册热键的编号      
  16.             );    

 

 

用RegisterHotKey注册的热键即时在失去焦点的情况下也可以有效!

所有实现代码如下:

 

C#代码
  1. using System;      
  2. using System.ComponentModel;      
  3. using System.Drawing;      
  4. using System.Text;      
  5. using System.Windows.Forms;      
  6.      
  7. using System.Runtime.InteropServices;      
  8.      
  9. namespace HotKeyTest      
  10. {      
  11.     public partial class Form1 : Form      
  12.     {      
  13.         public Form1()      
  14.         {      
  15.             InitializeComponent();      
  16.         }      
  17.      
  18.         private enum MyKeys      
  19.         {       
  20.             None = 0,      
  21.             Alt = 1,      
  22.             Ctrl = 2,      
  23.             Shift = 4,      
  24.             Win = 8      
  25.         }      
  26.      
  27.         const int WM_HOTKEY = 0x312;      
  28.      
  29.         private void Form1_Load(object sender, EventArgs e)      
  30.         {      
  31.             RegisterHotKey(this.Handle, 200, (int)MyKeys.Alt, (int)Keys.A); //注册热键Alt+A      
  32.             //RegisterHotKey(this.Handle, 200,(int)MyKeys.Ctrl|(int)MyKeys.Alt, (int)Keys.A); 注册热键Ctrl+Alt+A      
  33.             //RegisterHotKey(this.Handle, 200, (int)MyKeys.None, (int)Keys.F2); 注册热键F2      
  34.         }      
  35.      
  36.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)      
  37.         {      
  38.             UnregisterHotKey(this.Handle, 200); //注销热键      
  39.         }      
  40.      
  41.         private void btnTest_Click(object sender, EventArgs e)      
  42.         {      
  43.             MessageBox.Show("btnTest单击事件被触发!");      
  44.         }      
  45.      
  46.         protected override void WndProc(ref Message m)      
  47.         {      
  48.             if (m.Msg == WM_HOTKEY)      
  49.             {      
  50.                 switch (m.WParam.ToInt32())      
  51.                 {       
  52.                     case 200:      
  53.                         btnTest_Click(this, EventArgs.Empty);      
  54.                         break;      
  55.                 }      
  56.             }      
  57.      
  58.             base.WndProc(ref m);      
  59.         }      
  60.      
  61.         [DllImportAttribute("user32.dll", EntryPoint = "RegisterHotKey")]      
  62.         public static extern bool RegisterHotKey      
  63.             (      
  64.                 IntPtr hWnd,        //要注册热键的窗口句柄      
  65.                 int id,             //热键编号      
  66.                 int fsModifiers,    //特殊键如:Ctrl,Alt,Shift,Window      
  67.                 int vk              //一般键如:A B C F1,F2 等      
  68.             );      
  69.      
  70.         [DllImportAttribute("user32.dll", EntryPoint = "UnregisterHotKey")]      
  71.         public static extern bool UnregisterHotKey      
  72.             (      
  73.                 IntPtr hWnd,        //注册热键的窗口句柄      
  74.                 int id              //热键编号上面注册热键的编号      
  75.             );      
  76.     }      
  77. }    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值