热键枚举Winform程序全局热键与局部热键(键盘快捷键的捕获)

最近研究热键枚举,稍微总结一下,以后继续补充:

Winform程序全局热键

    Winform程序全局热键一般采取的是,调用windows api的情势,主要是通过面下的四个函数

            /// <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,
            UInt32 id,
            UInt32 fsModifiers,
            UInt32 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,             UInt32 id             );

 

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

 

            /// <summary>             /// 从全局子原表删除指定一个符字串,并回返这个符字串的一唯识标符,失败回返0             /// </summary>             /// <param name="nAtom"></param>             /// <returns></returns>             [DllImport("kernel32", SetLastError = true)]             public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);

    
还有一个帮助类,志标户用否是册注了含有win ctrl shift等特别键的快捷式方

/// <summary> 
            /// 定义了帮助键的名称(将数字转变为符字以便于记忆,也可去除此枚举而直接应用数值) 
            /// </summary>
            public enum KeyFlags
            {
                MOD_NONE = 0x0,
                MOD_ALT = 0x1,
                MOD_CONTROL = 0x2,
                MOD_SHIFT = 0x4,
                MOD_WIN = 0x8
            }

    
贴上部全代码:

 

    每日一道理
父亲对于儿子来说,是座耸立的高山,而儿子只是颗石子,源于山,却并不了解山。生活中诸多爱的密码,是需用细节来解读的,在亲情的沃土上,要想搞得最美的果实,惟有期待那存在于瞬间的心与心的共鸣,爱与爱的默契。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;

namespace luolunz
{
    public delegate void HotkeyEventHandler(int hotKeyID);

    public class HotkeyHelper : IMessageFilter
    {
        public event HotkeyEventHandler OnHotkey;

        public enum KeyFlags
        {
            MOD_NONE=0x0,
            MOD_ALT = 0x1,
            MOD_CONTROL = 0x2,
            MOD_SHIFT = 0x4,
            MOD_WIN = 0x8   
        }

        class NativeMethods
        {
            private NativeMethods() { }

            #region WIN32 API
            /// <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,
            UInt32 id,
            UInt32 fsModifiers,
            UInt32 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,
            UInt32 id
            );

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

            /// <summary>
            /// 从全局子原表删除指定一个符字串,并回返这个符字串的一唯识标符,失败回返0
            /// </summary>
            /// <param name="nAtom"></param>
            /// <returns></returns>
            [DllImport("kernel32", SetLastError = true)]
            public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);

            /// <summary> 
            /// 定义了帮助键的名称(将数字转变为符字以便于记忆,也可去除此枚举而直接应用数值) 
            /// </summary>
            public enum KeyFlags
            {
                MOD_NONE = 0x0,
                MOD_ALT = 0x1,
                MOD_CONTROL = 0x2,
                MOD_SHIFT = 0x4,
                MOD_WIN = 0x8
            }
            #endregion
        }

        Hashtable keyIDs = new Hashtable();
        IntPtr hWnd;

        public HotkeyHelper(IntPtr hWnd)
        {
            this.hWnd = hWnd;
            Application.AddMessageFilter(this);
        }

        public UInt32RegisterHotkey(Keys Key, KeyFlags keyflags)
        {
            UInt32 hotkeyid = NativeMethods.GlobalAddAtom(System.Guid.NewGuid().ToString());
            NativeMethods.RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
            keyIDs.Add(hotkeyid, hotkeyid);
            return (UInt32)hotkeyid;
        }

        public void UnregisterHotkeys()
        {
            Application.RemoveMessageFilter(this);
            foreach (UInt32 key in keyIDs.Values)
            {
                NativeMethods.UnregisterHotKey(hWnd, key);
                NativeMethods.GlobalDeleteAtom(key);
            }
        }

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == 0x312)
            {
                if (OnHotkey != null)
                {
                    foreach (UInt32 key in keyIDs.Values)
                    {
                        if ((UInt32)m.WParam == key)
                        {
                            OnHotkey((int)m.WParam);
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}

    应用式方:

HotkeyHelper HotkeyHelper { set; get; }
       	   UInt32 CONTROR_F;
            UInt32 CONTROR_G;
            UInt32 F10;
            HotkeyHelper = new HotkeyHelper(this.Handle);
            CONTROR_F = HotkeyHelper.RegisterHotkey(Keys.F, HotkeyHelper.KeyFlags.MOD_CONTROL | HotkeyHelper.KeyFlags.MOD_SHIFT);
            CONTROR_G = HotkeyHelper.RegisterHotkey(Keys.G, HotkeyHelper.KeyFlags.MOD_CONTROL);
            F10 = HotkeyHelper.RegisterHotkey(Keys.F10, 0);
            HotkeyHelper.OnHotkey += new HotkeyEventHandler(OnHotkey);

        private void OnHotkey(int hotkeyID)
        {
            if (hotkeyID == CONTROR_F)
            {
                //do something
                MessageBox.Show("CONTROR_SHIT_F");
            }
            else if (hotkeyID == F10)
            {
                //do something else
                MessageBox.Show("F10");
            }
        }

    
局部热键(盘键快捷键的捕获)

/// <summary>
        /// 户用盘键快捷键取获与处置
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            KeyEventArgs e = new KeyEventArgs(keyData);
            
            if (keyData == (Keys.Alt | Keys.P))
            {
                //do something
}
            if (keyData == (Keys.Alt | Keys.C))
            {
                //do something
}
            if (keyData == (Keys.Alt | Keys.A))
            {
                //do something
}
            if (keyData == (Keys.Alt | Keys.Z))
            {
                //do something
}
            if (keyData == (Keys.F1))
            {
                //do something
}

            if (keyData == (Keys.F3))
            {
                //do something
}
            return true;
        }

    

 

文章结束给大家分享下程序员的一些笑话语录: 问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?  
  答:因为李艳红这种败类,所以我没女友!

你可能感兴趣的:(WinForm)