/// <summary>Determines whether the specified keyboard input should be allowed to be processed by the system.</summary>
        /// <remarks>Helps block unwanted keys and key combinations that could exit the app, make system changes, etc.</remarks>
        public static bool AllowKeyboardInput(bool alt, bool control, Keyboard.VirtualKeyStates key)
        {
            // Disallow various special keys.
            if (key <= Keyboard.VirtualKeyStates.VK_BACK || key == Keyboard.VirtualKeyStates.VK_NONAME ||
                key == Keyboard.VirtualKeyStates.VK_MENU || key == Keyboard.VirtualKeyStates.VK_PAUSE ||
                key == Keyboard.VirtualKeyStates.VK_HELP)
            {
                return(false);
            }

            // Disallow ranges of special keys.
            // Currently leaves volume controls enabled; consider if this makes sense.
            // Disables non-existing Keys up to 65534, to err on the side of caution for future keyboard expansion.
            if ((key >= Keyboard.VirtualKeyStates.VK_LWIN && key <= Keyboard.VirtualKeyStates.VK_SLEEP) ||
                (key >= Keyboard.VirtualKeyStates.VK_KANA && key <= Keyboard.VirtualKeyStates.VK_HANJA) ||
                (key >= Keyboard.VirtualKeyStates.VK_CONVERT && key <= Keyboard.VirtualKeyStates.VK_MODECHANGE) ||
                //(key >= VirtualKey.BROWSER_BACK && key <= VirtualKey.BROWSER_HOME) ||
                (key >= Keyboard.VirtualKeyStates.VK_MEDIA_NEXT_TRACK && key <= Keyboard.VirtualKeyStates.VK_LAUNCH_APP2) ||
                (key >= Keyboard.VirtualKeyStates.VK_PROCESSKEY && key <= (Keyboard.VirtualKeyStates) 65534))
            {
                return(false);
            }

            // Disallow specific key combinations. (These component keys would be OK on their own.)
            if ((alt && key == Keyboard.VirtualKeyStates.VK_TAB) ||
                (alt && key == Keyboard.VirtualKeyStates.VK_SPACE) ||
                (control && key == Keyboard.VirtualKeyStates.VK_ESCAPE))
            {
                return(false);
            }

            // Allow anything else (like letters, numbers, spacebar, braces, and so on).
            return(true);
        }
Пример #2
0
        private IntPtr KeyHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            Hook.KBDLLHOOKSTRUCT   hs = (Hook.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Hook.KBDLLHOOKSTRUCT));
            Message.WindowsMessage wm = (Message.WindowsMessage)wParam;
            if (nCode >= 0 && (_key == 0 || _key == hs.vkCode))
            {
                Keyboard.VirtualKeyStates key      = (Keyboard.VirtualKeyStates)hs.vkCode;
                Keyboard.ScanCodes        scanCode = (Keyboard.ScanCodes)hs.scanCode;
                KeyboardState.KeyStatus   status;
                switch (wm)
                {
                case Message.WindowsMessage.KEYDOWN:
                case Message.WindowsMessage.SYSKEYDOWN:
                    status = KeyboardState.KeyStatus.Down;
                    break;

                case Message.WindowsMessage.KEYUP:
                case Message.WindowsMessage.SYSKEYUP:
                    status = KeyboardState.KeyStatus.Up;
                    break;

                default:
                    status = KeyboardState.KeyStatus.None;
                    break;
                }
                TimeSpan      time = TimeSpan.FromMilliseconds(hs.time);
                KeyboardState ks   = KeyboardState.CreateCurrent(key, scanCode, status, time);
                switch (ks.Status)
                {
                case KeyboardState.KeyStatus.Down:
                    KeyAny?.Invoke(this, ks);
                    KeyPress?.Invoke(this, ks);
                    KeyDown?.Invoke(this, ks);
                    break;

                case KeyboardState.KeyStatus.Hold:
                    KeyAny?.Invoke(this, ks);
                    KeyPress?.Invoke(this, ks);
                    KeyHold?.Invoke(this, ks);
                    break;

                case KeyboardState.KeyStatus.Up:
                    KeyAny?.Invoke(this, ks);
                    KeyUp?.Invoke(this, ks);
                    break;

                default:
                    KeyAny?.Invoke(this, ks);
                    break;
                }
                if (CallNextHook is null || CallNextHook(this, ks))
                {
                    return(Hook.CallNextHookEx(_kHHook, nCode, wParam, lParam));
                }
                else
                {
                    return(_kHHook);
                }
            }
Пример #3
0
 public static Keyboard.ScanCodes GetScanCode(Keyboard.VirtualKeyStates virtualKey)
 {
     if (Enum.TryParse((virtualKey & Keyboard.VirtualKeyStates.KeyCode).ToString(), out Keyboard.ScanCodes scanCode))
     {
         return(scanCode);
     }
     return(Keyboard.ScanCodes.None);
 }
 public KeyboardState(
     Keyboard.VirtualKeyStates key = Keyboard.VirtualKeyStates.None,
     Keyboard.ScanCodes scanCode   = Keyboard.ScanCodes.None,
     KeyStatus status       = KeyStatus.None,
     KeyModifiers modifiers = KeyModifiers.None,
     TimeSpan time          = new TimeSpan())
 {
     _Key       = key;
     _ScanCode  = scanCode;
     _Status    = status;
     _Modifiers = modifiers;
     _Time      = time;
 }
Пример #5
0
 public MouseState(
     Keyboard.VirtualKeyStates key = Keyboard.VirtualKeyStates.None,
     MouseStatus status            = MouseStatus.None,
     MouseModifiers modifiers      = MouseModifiers.None,
     int x         = 0,
     int y         = 0,
     int wheel     = 0,
     int hWheel    = 0,
     TimeSpan time = new TimeSpan())
 {
     _Key       = key;
     _Status    = status;
     _Modifiers = modifiers;
     _X         = x;
     _Y         = y;
     _Wheel     = wheel;
     _HWheel    = hWheel;
     _Time      = time;
 }
Пример #6
0
 public KeyboardHook(Keyboard.VirtualKeyStates vkCode = Keyboard.VirtualKeyStates.None)
 {
     _kProc = KeyHookProc;
     _key   = (int)vkCode;
 }
Пример #7
0
 public virtual void Up(Keyboard.VirtualKeyStates virtualKey, Keyboard.ScanCodes scanCode)
 {
     scanCode = GetClearScanCode(scanCode) | Keyboard.ScanCodes.Pressed | Keyboard.ScanCodes.Transition;
     Message.PostMessage(WindowHandle, Message.WindowsMessage.KEYUP, (IntPtr)virtualKey, GetLParam(scanCode));
 }
Пример #8
0
        public void Hold(Keyboard.VirtualKeyStates virtualKey)
        {
            var scanCode = GetScanCode(virtualKey);

            Hold(virtualKey, scanCode);
        }
Пример #9
0
 public virtual void Hold(Keyboard.VirtualKeyStates virtualKey, Keyboard.ScanCodes scanCode)
 {
     scanCode = GetClearScanCode(scanCode) | Keyboard.ScanCodes.Pressed;
     Message.PostMessage(WindowHandle, Message.WindowsMessage.KEYDOWN, (IntPtr)virtualKey, GetLParam(scanCode));
 }
Пример #10
0
        public void Down(Keyboard.VirtualKeyStates virtualKey)
        {
            var scanCode = GetScanCode(virtualKey);

            Down(virtualKey, scanCode);
        }
Пример #11
0
 public override void Down(Keyboard.VirtualKeyStates virtualKey, Keyboard.ScanCodes scanCode)
 {
     scanCode = GetClearScanCode(scanCode);
     Message.PostMessage(WindowHandle, Message.WindowsMessage.SYSKEYDOWN, (IntPtr)virtualKey, GetLParam(scanCode));
 }