C#热键

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

 用:

private void Form1_KeyDown(object sender, KeyEventArgs e)   
{   
    if (e.Control && e.KeyCode == Keys.A)   
    {   
        btnTest_Click(this, EventArgs.Empty);   
    }   
}  

 

 

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

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

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

 

[DllImportAttribute("user32.dll", EntryPoint = "RegisterHotKey")]   
        public static extern bool RegisterHotKey   
            (   
                IntPtr hWnd,        //要注册热键的窗口句柄   
                int id,             //热键编号   
                int fsModifiers,    //特殊键如:Ctrl,Alt,Shift,Window   
                int vk              //一般键如:A B C F1,F2 等   
            );   
  
  
        [DllImportAttribute("user32.dll", EntryPoint = "UnregisterHotKey")]   
        public static extern bool UnregisterHotKey   
            (   
                IntPtr hWnd,        //注册热键的窗口句柄   
                int id              //热键编号上面注册热键的编号   
            );  

 

 

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

所有实现代码如下:

 

using System;   
using System.ComponentModel;   
using System.Drawing;   
using System.Text;   
using System.Windows.Forms;   
  
using System.Runtime.InteropServices;   
  
namespace HotKeyTest   
{   
    public partial class Form1 : Form   
    {   
        public Form1()   
        {   
            InitializeComponent();   
        }   
  
        private enum MyKeys   
        {    
            None = 0,   
            Alt = 1,   
            Ctrl = 2,   
            Shift = 4,   
            Win = 8   
        }   
  
        const int WM_HOTKEY = 0x312;   
  
        private void Form1_Load(object sender, EventArgs e)   
        {   
            RegisterHotKey(this.Handle, 200, (int)MyKeys.Alt, (int)Keys.A); //注册热键Alt+A   
            //RegisterHotKey(this.Handle, 200,(int)MyKeys.Ctrl|(int)MyKeys.Alt, (int)Keys.A); 注册热键Ctrl+Alt+A   
            //RegisterHotKey(this.Handle, 200, (int)MyKeys.None, (int)Keys.F2); 注册热键F2   
        }   
  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)   
        {   
            UnregisterHotKey(this.Handle, 200); //注销热键   
        }   
  
        private void btnTest_Click(object sender, EventArgs e)   
        {   
            MessageBox.Show("btnTest单击事件被触发!");   
        }   
  
        protected override void WndProc(ref Message m)   
        {   
            if (m.Msg == WM_HOTKEY)   
            {   
                switch (m.WParam.ToInt32())   
                {    
                    case 200:   
                        btnTest_Click(this, EventArgs.Empty);   
                        break;   
                }   
            }   
  
            base.WndProc(ref m);   
        }   
  
        [DllImportAttribute("user32.dll", EntryPoint = "RegisterHotKey")]   
        public static extern bool RegisterHotKey   
            (   
                IntPtr hWnd,        //要注册热键的窗口句柄   
                int id,             //热键编号   
                int fsModifiers,    //特殊键如:Ctrl,Alt,Shift,Window   
                int vk              //一般键如:A B C F1,F2 等   
            );   
  
        [DllImportAttribute("user32.dll", EntryPoint = "UnregisterHotKey")]   
        public static extern bool UnregisterHotKey   
            (   
                IntPtr hWnd,        //注册热键的窗口句柄   
                int id              //热键编号上面注册热键的编号   
            );   
    }   
}  

 

你可能感兴趣的:(C++,c,windows,C#)