C# 键盘事件拦截与处理

private const byte KEY_A = 65;
        private const int KEY_A = 65;
        private const int KEY_B = 66;
        private const int KEY_C = 67;
        private const int KEY_D = 68;
        private const int KEY_E = 69;
        private const int KEY_F = 70;
        private const int KEY_G = 71;
        private const int KEY_H = 72;
        private const int KEY_I = 73;
        private const int KEY_J = 74;
        private const int KEY_K = 75;
        private const int KEY_L = 76;
        private const int KEY_M = 77;
        private const int KEY_N = 78;
        private const int KEY_O = 79;
        private const int KEY_P = 80;
        private const int KEY_Q = 81;
        private const int KEY_R = 82;
        private const int KEY_S = 83;
        private const int KEY_T = 84;
        private const int KEY_U = 85;
        private const int KEY_V = 86;
        private const int KEY_W = 87;
        private const int KEY_X = 88;
        private const int KEY_Y = 89;
        private const int KEY_Z = 90;

        private const int KEY_CTRL_LEFT = 162;
        private const int KEY_CTRL_RIGHT = 163;
        private const int KEY_ALT_LEFT = 164;
        private const int KEY_ALT_RIGHT = 165;
        private const int KEY_WIN_LEFT = 91;
        private const int KEY_WIN_RIGHT = 92;
        private const int KEY_MOUSE_RIGHT = 2;
        private const int KEY_SHIFT_LEFT = 160;
        private const int KEY_SHIFT_RIGHT = 161;

        private const int KEY_ZERO = 48;
        private const int KEY_ONE = 49;
        private const int KEY_TWO = 50;
        private const int KEY_THREE = 51;
        private const int KEY_FOUR = 52;
        private const int KEY_FIVE = 53;
        private const int KEY_SIX = 54;
        private const int KEY_SEVEN = 55;
        private const int KEY_EIGHT = 56;
        private const int KEY_NINE = 57;

        private const int KEY_MINUS = 189;//-_
        private const int KEY_PLUS = 187;//=+
        private const int KEY_BRACE_LEFT = 219;//[{
        private const int KEY_BRACE_RIGHT = 221;//]}
        private const int KEY_SLASH_RESERVE = 220;//\|
        private const int KEY_SEMICOLON = 186; //;:
        private const int KEY_QUOTE = 222;//'"
        private const int KEY_COMMA = 188;//,<
        private const int KEY_DOT = 190;//.>
        private const int KEY_SLASH = 191;///?
        private const int KEY_BACKQUOTE = 192;//`~

        private const int KEY_DELETE = 46;
        private const int KEY_ENTER = 13;
        private const int KEY_TAB = 9;
        private const int KEY_SPACE = 32;


        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        private static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;


        //LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,会在你截取之前获得键盘。 
        private static HookProc KeyBoardHookProcedure;
        //键盘Hook结构函数 
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        //设置钩子 
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        //抽掉钩子 
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll")]
        //调用下一个钩子 
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        public void Hook_Start()
        {
            // 安装键盘钩子 
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
                hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure,
                        GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                //如果设置钩子失败. 
                if (hHook == 0)
                {
                    Hook_Clear();
                }
            }
        }

        //取消钩子事件 
        public static void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            //如果去掉钩子失败. 
            if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
        }
        //这里可以添加自己想要的信息处理 
        private int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));

                if (kbh.vkCode == (int)Keys.LShiftKey && (kbh.flags & 0b10000000) == 0)
                {
                    ChineseLaoShift();
                    return 1;
                }
                else if (kbh.vkCode == (int)Keys.LWin && (kbh.flags & 0b10000000) == 0)
                {
                    ChineseLaoShift();
                    return 1;
                }

            }

            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }

结构体KeyBoardHookStruct官方说明:KBDLLHOOKSTRUCT (winuser.h) - Win32 apps | Microsoft Docs

主要看其flag属性:最高位也就是第7位(从右往左),1表示键盘被释放,0表示键盘被按下,可以用来做键盘事件类型的判断。

参考链接:C# 屏蔽windows功能键 - 冰魂雪魄 - 博客园

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值