Exemplo n.º 1
0
        /// <summary>  键盘钩子处理函数
        /// </summary>
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //为 2 ,直接返回,不响应键盘事件

            if (flags == HookType.Keyboard || flags == HookType.All)
            {
            }
            else
            {
                return(Hocy_Hook.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam));
            }



            bool handled = false;

            //it was ok and someone listens to events
            if ((nCode >= 0) && (this.OnKeyDown != null || this.OnKeyUp != null || this.OnKeyPress != null))
            {
                //read structure KeyboardHookStruct at lParam
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                //raise KeyDown
                if (this.OnKeyDown != null && (wParam == (int)WM_KEYBOARD.WM_KEYDOWN || wParam == (int)WM_KEYBOARD.WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VKCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    this.OnKeyDown(this, e);
                    handled = handled || e.Handled;
                }

                // raise KeyPress
                if (this.OnKeyPress != null && wParam == (int)WM_KEYBOARD.WM_KEYDOWN)
                {
                    bool isDownShift, isDownCapslock;
                    try
                    {
                        isDownShift    = ((Hocy_Hook.GetKeyStates(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                        isDownCapslock = (Hocy_Hook.GetKeyStates(VK_CAPITAL) != 0 ? true : false);
                    }
                    catch
                    {
                        isDownCapslock = false;
                        isDownShift    = false;
                    }

                    byte[] keyState = new byte[256];
                    Hocy_Hook.GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (Hocy_Hook.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);
                        }
                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        this.OnKeyPress(this, e);
                        handled = handled || e.Handled;
                    }
                }
                // raise KeyUp
                if (this.OnKeyUp != null && (wParam == (int)WM_KEYBOARD.WM_KEYUP || wParam == (int)WM_KEYBOARD.WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VKCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    this.OnKeyUp(this, e);
                    handled = handled || e.Handled;
                }
            }
            //handled 为true, 则其他程序无法接受到键盘事件
            if (handled)
            {
                return(1);
            }
            else
            {
                return(Hocy_Hook.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam));
            }
        }
Exemplo n.º 2
0
 /// <summary> 钩子类,实现 键盘和鼠标的钩子
 /// </summary>
 public Hocy_Hook()
 {
     Hocy_Hook.GetKeyboardState(this.m_KeyState);
 }