Пример #1
0
        /// <summary>
        ///     The callback for the keyboard hook.</summary>
        /// <param name="code">
        ///     The hook code. If this is &lt; 0, the callback shouldn’t do anyting.</param>
        /// <param name="wParam">
        ///     The event type. Only <c>WM_(SYS)?KEY(DOWN|UP)</c> events are handled.</param>
        /// <param name="lParam">
        ///     Information about the key pressed/released.</param>
        private int hookProc(int code, int wParam, ref WinAPI.KeyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Keys key = (Keys) lParam.vkCode;

                if (HookAllKeys || _hookedKeys.Contains(key))
                {
                    if ((wParam == WinAPI.WM_KEYDOWN || wParam == WinAPI.WM_SYSKEYDOWN))
                    {
                        switch (key)
                        {
                            case Keys.ControlKey:
                            case Keys.LControlKey:
                            case Keys.RControlKey: _ctrl = true; break;

                            case Keys.Menu:
                            case Keys.LMenu:
                            case Keys.RMenu: _alt = true; break;

                            case Keys.ShiftKey:
                            case Keys.LShiftKey:
                            case Keys.RShiftKey: _shift = true; break;

                            case Keys.LWin:
                            case Keys.RWin: _win = true; break;
                        }
                        if (KeyDown != null)
                        {
                            var kea = new GlobalKeyEventArgs(key, lParam.scanCode, new ModifierKeysState(_ctrl, _alt, _shift, _win));
                            KeyDown(this, kea);
                            if (kea.Handled)
                                return 1;
                        }
                    }
                    else if ((wParam == WinAPI.WM_KEYUP || wParam == WinAPI.WM_SYSKEYUP))
                    {
                        switch (key)
                        {
                            case Keys.ControlKey:
                            case Keys.LControlKey:
                            case Keys.RControlKey: _ctrl = false; break;

                            case Keys.Menu:
                            case Keys.LMenu:
                            case Keys.RMenu: _alt = false; break;

                            case Keys.ShiftKey:
                            case Keys.LShiftKey:
                            case Keys.RShiftKey: _shift = false; break;

                            case Keys.LWin:
                            case Keys.RWin: _win = false; break;
                        }
                        if (KeyUp != null)
                        {
                            var kea = new GlobalKeyEventArgs(key, lParam.scanCode, new ModifierKeysState(_ctrl, _alt, _shift, _win));
                            KeyUp(this, kea);
                            if (kea.Handled)
                                return 1;
                        }
                    }
                }
            }
            return WinAPI.CallNextHookEx(_hHook, code, wParam, ref lParam);
        }
Пример #2
0
        /// <summary>
        ///     The callback for the keyboard hook.</summary>
        /// <param name="code">
        ///     The hook code. If this is &lt; 0, the callback shouldn’t do anyting.</param>
        /// <param name="wParam">
        ///     The event type. Only <c>WM_(SYS)?KEY(DOWN|UP)</c> events are handled.</param>
        /// <param name="lParam">
        ///     Information about the key pressed/released.</param>
        private int hookProc(int code, int wParam, ref WinAPI.KeyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Keys key = (Keys)lParam.vkCode;

                if (HookAllKeys || _hookedKeys.Contains(key))
                {
                    if ((wParam == WinAPI.WM_KEYDOWN || wParam == WinAPI.WM_SYSKEYDOWN))
                    {
                        switch (key)
                        {
                        case Keys.ControlKey:
                        case Keys.LControlKey:
                        case Keys.RControlKey: _ctrl = true; break;

                        case Keys.Menu:
                        case Keys.LMenu:
                        case Keys.RMenu: _alt = true; break;

                        case Keys.ShiftKey:
                        case Keys.LShiftKey:
                        case Keys.RShiftKey: _shift = true; break;

                        case Keys.LWin:
                        case Keys.RWin: _win = true; break;
                        }
                        if (KeyDown != null)
                        {
                            var kea = new GlobalKeyEventArgs(key, lParam.scanCode, new ModifierKeysState(_ctrl, _alt, _shift, _win));
                            KeyDown(this, kea);
                            if (kea.Handled)
                            {
                                return(1);
                            }
                        }
                    }
                    else if ((wParam == WinAPI.WM_KEYUP || wParam == WinAPI.WM_SYSKEYUP))
                    {
                        switch (key)
                        {
                        case Keys.ControlKey:
                        case Keys.LControlKey:
                        case Keys.RControlKey: _ctrl = false; break;

                        case Keys.Menu:
                        case Keys.LMenu:
                        case Keys.RMenu: _alt = false; break;

                        case Keys.ShiftKey:
                        case Keys.LShiftKey:
                        case Keys.RShiftKey: _shift = false; break;

                        case Keys.LWin:
                        case Keys.RWin: _win = false; break;
                        }
                        if (KeyUp != null)
                        {
                            var kea = new GlobalKeyEventArgs(key, lParam.scanCode, new ModifierKeysState(_ctrl, _alt, _shift, _win));
                            KeyUp(this, kea);
                            if (kea.Handled)
                            {
                                return(1);
                            }
                        }
                    }
                }
            }
            return(WinAPI.CallNextHookEx(_hHook, code, wParam, ref lParam));
        }