在Winform和WPF中注册全局快捷键

快捷键辅助类

View Code
  class HotKey
    {

        /// <summary>
        /// 如果函数执行成功,返回值不为0。
        /// 如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。.NET方法:Marshal.GetLastWin32Error()
        /// </summary>
        /// <param name="hWnd">要定义热键的窗口的句柄</param>
        /// <param name="id">定义热键ID(不能与其它ID重复)  </param>
        /// <param name="fsModifiers">标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效</param>
        /// <param name="vk">定义热键的内容,WinForm中可以使用Keys枚举转换,
        /// WPF中Key枚举是不正确的,应该使用System.Windows.Forms.Keys枚举,或者自定义正确的枚举或int常量</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,
            int id,
            KeyModifiers fsModifiers,
            int vk
            );

        /// <summary>
        /// 取消注册热键
        /// </summary>
        /// <param name="hWnd">要取消热键的窗口的句柄</param>
        /// <param name="id">要取消热键的ID</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,
            int id
            );

        /// <summary>
        /// 向全局原子表添加一个字符串,并返回这个字符串的唯一标识符,成功则返回值为新创建的原子ID,失败返回0
        /// </summary>
        /// <param name="lpString"></param>
        /// <returns></returns>
        [DllImport("kernel32", SetLastError = true)]
        public static extern short GlobalAddAtom(string lpString);

        [DllImport("kernel32", SetLastError = true)]
        public static extern short GlobalDeleteAtom(short nAtom);

        /// <summary>
        /// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
        /// </summary>
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }
        /// <summary>
        /// 热键的对应的消息ID
        /// </summary>
        public const int WM_HOTKEY = 0x312;
    }

WinForm方法:

在中在onload中注册事件,然后重载WndProc,对返回的消息进行处理

View Code
        int alts,altd;
        protected override void OnLoad(EventArgs e)
        {
            alts= HotKey.GlobalAddAtom("Alt-S");
            altd= HotKey.GlobalAddAtom("Alt-D");
            HotKey.RegisterHotKey(this.Handle, alts, HotKey.KeyModifiers.Alt, (int)Keys.S);
            HotKey.RegisterHotKey(this.Handle, altd, HotKey.KeyModifiers.Alt, (int)Keys.D);
        }

        protected override void WndProc(ref Message m)// 监视Windows消息
        {
            switch (m.Msg)
            {
                case HotKey.WM_HOTKEY:
                    ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
                    break;
            }
            base.WndProc(ref m); //将系统消息传递自父类的WndProc
        }

         private void ProcessHotkey(Message m) //按下设定的键时调用该函数
        {
            IntPtr id = m.WParam;//IntPtr用于表示指针或句柄的平台特定类型
             int sid=id.ToInt32();
            if(sid==alts)
            {
                MessageBox.Show("按下Alt+S");
            }
            else if(sid==altd)
            {
                MessageBox.Show("按下Alt+D");
            }
        }

也可以在Application使用AddMessageFilter添加处理来代替重载WndProc,可以使form自身实现IMessageFilter接口,

注册方法

View Code
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form1();
            Application.AddMessageFilter(form);
            Application.Run(form);
        }

实现接口

View Code
         bool IMessageFilter.PreFilterMessage(ref Message m)
         {
             const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
             switch (m.Msg)
             {
                 case WM_HOTKEY:
                     ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
                     break;
             }
             //如果筛选消息并禁止消息被调度,则为 true;如果允许消息继续到达下一个筛选器或控件,则为 false
             return false ;
         }

如果同时在AddMessageFilter和WndProc处理消息,顺序是先IMessageFilter,然后调用WndProc

WPF方法:

WPF中的注册方法和Winform一样,只是WPF的Key枚举的值不能正确监听,要引用System.Windows.Forms.Keys或者自己定义才可以正确注册,窗口句柄也需要借助WindowInteropHelper来得到,处理函数的加入也和Winform不同,需要HwndSource来添加处理函数。

注册及处理方法

View Code
        int alts, altd;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            HwndSource hWndSource;
            WindowInteropHelper wih = new WindowInteropHelper(this);
            hWndSource = HwndSource.FromHwnd(wih.Handle);
            //添加处理程序
            hWndSource.AddHook(MainWindowProc);

            alts = HotKey.GlobalAddAtom("Alt-S");
            altd = HotKey.GlobalAddAtom("Alt-D");
            HotKey.RegisterHotKey(wih.Handle, alts, HotKey.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.S);
            HotKey.RegisterHotKey(wih.Handle, altd, HotKey.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.D);

        }

        private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case HotKey.WM_HOTKEY:
                    {
                        int sid = wParam.ToInt32();
                        if (sid == alts)
                        {
                            MessageBox.Show("按下Alt+S");
                        }
                        else if (sid == altd)
                        {
                            MessageBox.Show("按下Alt+D");
                        }
                        handled = true;
                        break;
                    }
            }

            return IntPtr.Zero;
        }

注意:

如果注册快捷键,RegisterHotKey中的fsModifiers参数为0,即None选项,一些安全软件会警报,可能因为这样就可以全局监听键盘而造成安全问题

代码下载

原文地址:https://www.cnblogs.com/FlyCat/p/2708908.html