/// <summary>
        /// 判断是否执行键盘的单击操作
        /// </summary>
        /// <param wParam="IntPtr">键盘的操作值</param>
        /// <param lParam="IntPtr">指针</param>
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool handled = false;
            EventHandler <KeyExEventArgs>      handlerKeyDown  = this.Events[EventKeyDown] as EventHandler <KeyExEventArgs>;       //实例化键盘的按下操作
            EventHandler <KeyExEventArgs>      handlerKeyUp    = this.Events[EventKeyUp] as EventHandler <KeyExEventArgs>;         //实例化键盘的松开操作
            EventHandler <KeyPressExEventArgs> handlerKeyPress = this.Events[EventKeyPress] as EventHandler <KeyPressExEventArgs>; //实例化键盘的单击操作

            //如果键盘被操作
            if ((nCode >= 0) && (handlerKeyDown != null || handlerKeyUp != null || handlerKeyPress != null))
            {
                //实例化KeyboardHookStruct类
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                //如果键盘按下
                if (handlerKeyDown != null && ((int)wParam == WM_KEYDOWN || (int)wParam == WM_SYSKEYDOWN))
                {
                    Keys           keyData = (Keys)MyKeyboardHookStruct.vkCode;                       //获取键值
                    KeyExEventArgs e       = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags); //实例化KeyExEventArgs
                    handlerKeyDown(this, e);                                                          //执行键盘按下操作
                    handled = handled || e.Handled;                                                   //是否操作
                }
                if (handlerKeyPress != null && (int)wParam == WM_KEYDOWN)                             //如果单击键盘
                {
                    bool   isDownShift    = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);  //判断是否为SHIFT键
                    bool   isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);            //判断是否为CAPS LOCK键
                    byte[] keyState       = new byte[256];                                            //定义字节数组
                    GetKeyboardState(keyState);                                                       //获取虚拟键的当前状态
                    byte[] inBuffer = new byte[2];                                                    //定义字节数组
                    //如果可以将虚拟键转换成ASCII字符
                    if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
                    {
                        char key = (char)inBuffer[0];                                                     //获取ASCII字符
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))                         //如果将其转换成大写
                        {
                            key = Char.ToUpper(key);                                                      //获取大写的ASCII字符
                        }
                        KeyPressExEventArgs e = new KeyPressExEventArgs(key, MyKeyboardHookStruct.flags); //实例化KeyPressExEventArgs
                        handlerKeyPress(this, e);                                                         //执行键盘单击事件
                        handled = handled || e.Handled;                                                   //是否操作
                    }
                }
                if (handlerKeyUp != null && ((int)wParam == WM_KEYUP || (int)wParam == WM_SYSKEYUP))  //如果松开键盘
                {
                    Keys           keyData = (Keys)MyKeyboardHookStruct.vkCode;                       //获取键值
                    KeyExEventArgs e       = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags); //实例化KeyExEventArgs类
                    handlerKeyUp(this, e);                                                            //执行键盘的松开操作
                    handled = handled || e.Handled;                                                   //是否操作
                }
            }
            if (handled)//如果不对当前进行操作
            {
                return((IntPtr)1);
            }
            else
            {
                return(CallNextHookEx(hKeyboardHook, nCode, wParam, lParam));
            }
        }
Esempio n. 2
0
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            EventHandler <KeyExEventArgs>      handlerKeyDown  = this.Events[EventKeyDown] as EventHandler <KeyExEventArgs>;
            EventHandler <KeyExEventArgs>      handlerKeyUp    = this.Events[EventKeyUp] as EventHandler <KeyExEventArgs>;
            EventHandler <KeyPressExEventArgs> handlerKeyPress = this.Events[EventKeyPress] as EventHandler <KeyPressExEventArgs>;

            //it was ok and someone listens to events
            if ((nCode >= 0) && (handlerKeyDown != null || handlerKeyUp != null || handlerKeyPress != null))
            {
                //read structure KeyboardHookStruct at lParam
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                //raise KeyDown
                if (handlerKeyDown != null && ((int)wParam == WM_KEYDOWN || (int)wParam == WM_SYSKEYDOWN))
                {
                    Keys           keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyExEventArgs e       = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags);
                    handlerKeyDown(this, e);
                    handled = handled || e.Handled;
                }

                // raise KeyPress
                if (handlerKeyPress != null && (int)wParam == WM_KEYDOWN)
                {
                    bool isDownShift    = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (ToAscii(MyKeyboardHookStruct.vkCode,
                                MyKeyboardHookStruct.scanCode,
                                keyState,
                                inBuffer,
                                MyKeyboardHookStruct.flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }
                        KeyPressExEventArgs e = new KeyPressExEventArgs(key, MyKeyboardHookStruct.flags);
                        handlerKeyPress(this, e);
                        handled = handled || e.Handled;
                    }
                }

                // raise KeyUp
                if (handlerKeyUp != null && ((int)wParam == WM_KEYUP || (int)wParam == WM_SYSKEYUP))
                {
                    Keys           keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyExEventArgs e       = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags);

                    handlerKeyUp(this, e);
                    handled = handled || e.Handled;
                }
            }

            //if event handled in application do not handoff to other listeners
            if (handled)
            {
                return((IntPtr)1);
            }
            else
            {
                return(CallNextHookEx(hKeyboardHook, nCode, wParam, lParam));
            }
        }
Esempio n. 3
0
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            EventHandler<KeyExEventArgs> handlerKeyDown = this.Events[EventKeyDown] as EventHandler<KeyExEventArgs>;
            EventHandler<KeyExEventArgs> handlerKeyUp = this.Events[EventKeyUp] as EventHandler<KeyExEventArgs>;
            EventHandler<KeyPressExEventArgs> handlerKeyPress = this.Events[EventKeyPress] as EventHandler<KeyPressExEventArgs>;

            //it was ok and someone listens to events
            if ((nCode >= 0) && (handlerKeyDown != null || handlerKeyUp != null || handlerKeyPress != null)) {
                //read structure KeyboardHookStruct at lParam
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                //raise KeyDown
                if (handlerKeyDown != null && ((int)wParam == WM_KEYDOWN || (int)wParam == WM_SYSKEYDOWN)) {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyExEventArgs e = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags);
                    handlerKeyDown(this, e);
                    handled = handled || e.Handled;
                }

                // raise KeyPress
                if (handlerKeyPress != null && (int)wParam == WM_KEYDOWN) {
                    bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (ToAscii(MyKeyboardHookStruct.vkCode,
                          MyKeyboardHookStruct.scanCode,
                          keyState,
                          inBuffer,
                          MyKeyboardHookStruct.flags) == 1) {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                            key = Char.ToUpper(key);
                        KeyPressExEventArgs e = new KeyPressExEventArgs(key, MyKeyboardHookStruct.flags);
                        handlerKeyPress(this, e);
                        handled = handled || e.Handled;
                    }
                }

                // raise KeyUp
                if (handlerKeyUp != null && ((int)wParam == WM_KEYUP || (int)wParam == WM_SYSKEYUP)) {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyExEventArgs e = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags);

                    handlerKeyUp(this, e);
                    handled = handled || e.Handled;
                }

            }

            //if event handled in application do not handoff to other listeners
            if (handled)
                return (IntPtr)1;
            else
                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }