示例#1
0
 public virtual void OnKeyDown(KeyEventArgs e)
 {
     KeyDown?.Invoke(this, e);
 }
 static KeyboardInputManager()
 {
     InputHooks.KeyDown  += (o, e) => KeyDown?.Invoke(new KeyboardHookEventArgs((int)e.KeyData));
     InputHooks.KeyUp    += (o, e) => KeyUp?.Invoke(new KeyboardHookEventArgs((int)e.KeyData));
     InputHooks.KeyPress += (o, e) => KeyPress?.Invoke(new KeyPressHookEventArgs(e.KeyChar));
 }
示例#3
0
 public void OnKeyDown(KeyEventArgs keyEventArgs)
 {
     KeyDown?.Invoke(this, keyEventArgs);
 }
示例#4
0
 /// <summary>
 /// Raise the event <see cref="KeyDown"/>.
 /// </summary>
 protected virtual void OnKeyDown(KeyCode key)
 {
     _KeysPressed[(int)key] = true;
     KeyDown?.Invoke(this, new NativeWindowKeyEventArgs(DeviceContext, GLContext, key));
 }
示例#5
0
        public Node <INode> GetNode(string nodeName)
        {
            switch (nodeName)
            {
            case DeltaTime.NAME:
                INode nodeDeltaTime = new DeltaTime() as INode;
                return(new Node <INode> (nodeDeltaTime));

            case KeyDown.NAME:
                INode nodeKeyDown = new KeyDown() as INode;
                return(new Node <INode> (nodeKeyDown));

            case Transform.NAME:
                INode nodeTransform = new Transform() as INode;
                return(new Node <INode> (nodeTransform));

            case Vec3.NAME:
                INode nodeVec3 = new Vec3() as INode;
                return(new Node <INode> (nodeVec3));

            case Key.NAME:
                INode nodeKey = new Key() as INode;
                return(new Node <INode> (nodeKey));

            case FindByName.NAME:
                INode nodeFindByName = new FindByName() as INode;
                return(new Node <INode> (nodeFindByName));

            case ObjectAttribute.NAME:
                INode nodeObjectAttribute = new ObjectAttribute() as INode;
                return(new Node <INode> (nodeObjectAttribute));

            case SplitVec3.NAME:
                INode nodeSplitVec3 = new SplitVec3() as INode;
                return(new Node <INode> (nodeSplitVec3));

            case Update.NAME:
                INode nodeUpdate = new Update() as INode;
                return(new Node <INode> (nodeUpdate));

            case LateUpdate.NAME:
                INode nodeLateUpdate = new LateUpdate() as INode;
                return(new Node <INode> (nodeLateUpdate));

            case MouseButtonDown.NAME:
                INode nodeMouseButtonDown = new MouseButtonDown() as INode;
                return(new Node <INode> (nodeMouseButtonDown));

            case PlayerPreferences.NAME:
                INode nodePlayerPref = new PlayerPreferences() as INode;
                return(new Node <INode> (nodePlayerPref));

            case SetActive.NAME:
                INode nodeSetActive = new SetActive() as INode;
                return(new Node <INode> (nodeSetActive));

            case LoadScene.NAME:
                INode nodeLoadScene = new LoadScene() as INode;
                return(new Node <INode> (nodeLoadScene));

            case MousePosition.NAME:
                INode nodeMousePosition = new MousePosition() as INode;
                return(new Node <INode> (nodeMousePosition));

            case ScreenToWorld.NAME:
                INode nodeScreenToWorld = new ScreenToWorld() as INode;
                return(new Node <INode> (nodeScreenToWorld));

            case LookAtPosition.NAME:
                INode nodeLookAt = new LookAtPosition() as INode;
                return(new Node <INode> (nodeLookAt));

            case GetComponent.NAME:
                INode nodeGetComponent = new GetComponent() as INode;
                return(new Node <INode> (nodeGetComponent));

            case Color.NAME:
                INode nodeColor = new Color() as INode;
                return(new Node <INode> (nodeColor));

            default:
                return(null);
            }
        }
示例#6
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(hookId, nCode, wParam, lParam));
            }

            if (wParam == (IntPtr)WM_KEYDOWN)
            {
                var vkCode = Marshal.ReadInt32(lParam);
                var k      = KeyShortcut.FromVirtualKey(vkCode);
                var m      = Keyboard.Modifiers;
//                if (!KeyShortcut.IsModifier(k.Key))
//                {
//
//                }
                KeyDown?.Invoke(new KeyShortcut {
                    Key = k, Modifiers = m
                });
//                if (oneTimeKeys != null)
//                {
//                    if (!KeyShortcut.IsModifier(k))
//                    {
//                        oneTimeKeys.Invoke(new KeyShortcut() {Key = k, Modifiers = m});
//                        oneTimeKeys = null;
//                        Close();
//                    }
//                    return CallNextHookEx(hookId, nCode, wParam, lParam);
//                }

                foreach (var rec in records)
                {
                    if (rec.shortcut.Key == k && rec.shortcut.Modifiers == m)
                    {
                        rec.action.Invoke();
                    }
                }
                Console.WriteLine($"{vkCode}[{m}]: {k}|{Thread.CurrentThread.ManagedThreadId}");
            }

//            if ((MainWindow.StartStopKey == (Keys) vkCode) && (MainWindow.Paused == false) &&
//                (wParam == (IntPtr) WM_KEYDOWN))
//            {
//                MainWindow.Paused = true;
//                Debug.WriteLine("Paused");
//                return CallNextHookEx(hookId, nCode, wParam, lParam);
//            }
//            if (MainWindow.StartStopKey == (Keys) vkCode && MainWindow.Paused && (wParam == (IntPtr) WM_KEYDOWN))
//            {
//                MainWindow.Paused = false;
//
//                return CallNextHookEx(hookId, nCode, wParam, lParam);
//            }


//            if (wParam == (IntPtr) WM_KEYDOWN && MainWindow.Paused == false)
//            {
//                string s = ((Keys) vkCode) + ",  ";
//                //konsola(s);
//
//                KeyDown(this, kea);
//
//            }
//            else if (wParam == (IntPtr) WM_KEYUP && MainWindow.Paused == false)
//            {
//                if ((Keys) vkCode == Keys.LMenu || (Keys) vkCode == Keys.RMenu)
//                {
//                    if (MainWindow.consoleEnabled)
//                        Console.Write(((Keys) vkCode).ToString() + " ");
//                    KeyDown(this, kea);
//                }
//            }
            return(CallNextHookEx(hookId, nCode, wParam, lParam));
        }
示例#7
0
 private void OnKeyDown(Key key)
 {
     KeyDown?.Invoke(this, key);
 }
示例#8
0
 /// <inheritdoc cref="KeyDown" />
 private void ControlOnKeyDown(object sender, System.Windows.Input.KeyEventArgs args) => KeyDown?.Invoke(sender, new KeyEventArgs(args));
示例#9
0
        private void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs e)
        {
            if ((e.EventType == CoreAcceleratorKeyEventType.SystemKeyDown ||
                 e.EventType == CoreAcceleratorKeyEventType.KeyDown))
            {
                var  coreWindow = Windows.UI.Xaml.Window.Current.CoreWindow;
                var  downState  = CoreVirtualKeyStates.Down;
                var  virtualKey = e.VirtualKey;
                bool winKey     = ((coreWindow.GetKeyState(VirtualKey.LeftWindows) & downState) == downState || (coreWindow.GetKeyState(VirtualKey.RightWindows) & downState) == downState);
                bool altKey     = (coreWindow.GetKeyState(VirtualKey.Menu) & downState) == downState;
                bool controlKey = (coreWindow.GetKeyState(VirtualKey.Control) & downState) == downState;
                bool shiftKey   = (coreWindow.GetKeyState(VirtualKey.Shift) & downState) == downState;

                // raise keydown actions
                var keyDown = new KeyboardEventArgs
                {
                    AltKey     = altKey,
                    Character  = ToChar(virtualKey, shiftKey),
                    ControlKey = controlKey,
                    EventArgs  = e,
                    ShiftKey   = shiftKey,
                    VirtualKey = virtualKey
                };

                try { KeyDown?.Invoke(keyDown); }
                catch { }

                // Only investigate further when Left, Right, or the dedicated Previous or Next keys
                // are pressed
                if (virtualKey == VirtualKey.Left ||
                    virtualKey == VirtualKey.Right ||
                    (int)virtualKey == 166 ||
                    (int)virtualKey == 167 ||
                    (int)virtualKey == 69)
                {
                    bool noModifiers = !altKey && !controlKey && !shiftKey;
                    bool onlyAlt     = altKey && !controlKey && !shiftKey;

                    if (((int)virtualKey == 166 && noModifiers) ||
                        (virtualKey == VirtualKey.Left && onlyAlt))
                    {
                        // When the previous key or Alt+Left are pressed navigate back
                        e.Handled = true;
                        RaiseGoBackGestured();
                    }
                    else if (virtualKey == VirtualKey.Back && winKey)
                    {
                        // When the next key or Win+Backspace are pressed navigate backward
                        e.Handled = true;
                        RaiseGoBackGestured();
                    }
                    else if (((int)virtualKey == 167 && noModifiers) || (virtualKey == VirtualKey.Right && onlyAlt))
                    {
                        // When the next key or Alt+Right are pressed navigate forward
                        e.Handled = true;
                        RaiseGoForwardGestured();
                    }
                    else if (((int)virtualKey == 69 && controlKey))
                    {
                        // when control-E
                        e.Handled = true;
                        RaiseControlEGestured();
                    }
                }
            }
        }
示例#10
0
 private void FireKeyDown(Keys key)
 {
     KeyDown?.Invoke(this, new KeyDownEventArgs(key));
 }
示例#11
0
        /// <summary>
        /// A callback function which will be called every time a keyboard activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //Indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            //If was Ok and someone listens to events
            if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
            {
                //Read structure KeyboardHookStruct at lParam
                var myKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    #region Raise KeyDown

                    var keyData = (Keys)myKeyboardHookStruct.vkCode;
                    var e       = new CustomKeyEventArgs(keyData);
                    KeyDown?.Invoke(this, e);

                    handled = handled || e.Handled;

                    #endregion
                }

                if (KeyPress != null && wParam == WM_KEYDOWN)
                {
                    #region Raise KeyPress

                    bool isDownShift    = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0);

                    var keyState = new byte[256];
                    GetKeyboardState(keyState);
                    var inBuffer = new byte[2];

                    if (ToAscii(myKeyboardHookStruct.vkCode, myKeyboardHookStruct.scanCode, keyState, inBuffer, myKeyboardHookStruct.flags) == 1)
                    {
                        var key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }

                        var e = new CustomKeyPressEventArgs(key);
                        KeyPress?.Invoke(this, e);

                        handled = handled || e.Handled;
                    }

                    #endregion
                }

                if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    #region Raise KeyUp

                    var keyData = (Keys)myKeyboardHookStruct.vkCode;
                    var e       = new CustomKeyEventArgs(keyData);
                    KeyUp?.Invoke(this, e);

                    handled = handled || e.Handled;

                    #endregion
                }
            }

            //If event handled in application do not handoff to other listeners
            if (handled)
            {
                return(1);
            }

            return(CallNextHookEx(hKeyboardHook, nCode, wParam, lParam));
        }
示例#12
0
 private void KD(InputControl c) => KeyDown?.Invoke(this, new InputEventArgs(c));
示例#13
0
        /// <summary>
        /// Analizes hot key message.
        /// Understands what keys have been pressed or released
        /// </summary>
        /// <param name="msg"></param>
        public void AnalyzeKeys(ZeemoteMessage msg)
        {
            // If there are buttons touched in message
            if (msg.ButtonsTouched)
            {
                if (!msg.ButtonA && curZeemoteKeys.A)
                {
                    curZeemoteKeys.A = msg.ButtonA;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeA));
                    }
                }
                else if (msg.ButtonA && !curZeemoteKeys.A)
                {
                    curZeemoteKeys.A = msg.ButtonA;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeA));
                    }
                }

                if (!msg.ButtonB && curZeemoteKeys.B)
                {
                    curZeemoteKeys.B = msg.ButtonB;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeB));
                    }
                }
                else if (msg.ButtonB && !curZeemoteKeys.B)
                {
                    curZeemoteKeys.B = msg.ButtonB;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeB));
                    }
                }

                if (!msg.ButtonC && curZeemoteKeys.C)
                {
                    curZeemoteKeys.C = msg.ButtonC;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeC));
                    }
                }
                else if (msg.ButtonC && !curZeemoteKeys.C)
                {
                    curZeemoteKeys.C = msg.ButtonC;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeC));
                    }
                }

                if (!msg.ButtonD && curZeemoteKeys.D)
                {
                    curZeemoteKeys.D = msg.ButtonD;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeD));
                    }
                }
                else if (msg.ButtonD && !curZeemoteKeys.D)
                {
                    curZeemoteKeys.D = msg.ButtonD;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeD));
                    }
                }
            }
            else // Joystick was touched
            {
                int direction = FindJoystickDirection(msg);

                // 0 - no; 1 - left; 2 - right; 3 - up; 4 - down.
                if (direction != 1 && curZeemoteKeys.JoyLeft)
                {
                    curZeemoteKeys.JoyLeft = false;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyLeft));
                    }
                }
                else if (direction == 1 && !curZeemoteKeys.JoyLeft)
                {
                    curZeemoteKeys.JoyLeft = true;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyLeft));
                    }
                }

                if (direction != 2 && curZeemoteKeys.JoyRight)
                {
                    curZeemoteKeys.JoyRight = false;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyRight));
                    }
                }
                else if (direction == 2 && !curZeemoteKeys.JoyRight)
                {
                    curZeemoteKeys.JoyRight = true;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyRight));
                    }
                }

                if (direction != 3 && curZeemoteKeys.JoyUp)
                {
                    curZeemoteKeys.JoyUp = false;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyUp));
                    }
                }
                else if (direction == 3 && !curZeemoteKeys.JoyUp)
                {
                    curZeemoteKeys.JoyUp = true;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyUp));
                    }
                }

                if (direction != 4 && curZeemoteKeys.JoyDown)
                {
                    curZeemoteKeys.JoyDown = false;
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyDown));
                    }
                }
                else if (direction == 4 && !curZeemoteKeys.JoyDown)
                {
                    curZeemoteKeys.JoyDown = true;
                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyDown));
                    }
                }
            }
        }
示例#14
0
 protected virtual void OnKeyDown(Key key) => KeyDown?.Invoke(key);
示例#15
0
        /// <summary>
        /// A callback function which will be called every time a keyboard activity detected.
        /// https://docs.microsoft.com/en-us/windows/win32/winmsg/lowlevelkeyboardproc
        /// </summary>
        /// <param name="code">
        /// Specifies whether the hook procedure must process the message.
        /// If code is HC_ACTION, the hook procedure must process the message.
        /// If code is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If code is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If code is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private IntPtr KeyboardHookProc(int code, uint wParam, IntPtr lParam)
        {
            //Indicates if any of the underlaying events set the e.Handled flag.
            var handled = false;

            //If it was Ok and there are no listeners.
            if (code < 0 || KeyDown == null && KeyUp == null && KeyPress == null)
            {
                return(CallNextHookEx(_keyboardHookHandle, code, wParam, lParam));
            }

            //Read structure KeyboardHookStruct at lParam
            var keyboard = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

            if (KeyDown != null && (wParam == MessageKeydown || wParam == MessageSystemKeyDown))
            {
                #region Raise KeyDown

                var isDownShift    = (GetKeyState(KeyShift) & 0x80) == 0x80;
                var isDownCapslock = GetKeyState(KeyCapital) != 0;

                var e = new CustomKeyEventArgs(KeyInterop.KeyFromVirtualKey(keyboard.KeyCode), isDownCapslock ^ isDownShift);

                KeyDown?.Invoke(this, e);

                handled = e.Handled;

                #endregion
            }

            if (KeyPress != null && wParam == MessageKeydown)
            {
                #region Raise KeyPress

                var isDownShift    = (GetKeyState(KeyShift) & 0x80) == 0x80;
                var isDownCapslock = GetKeyState(KeyCapital) != 0;

                var keyState = new byte[256];
                GetKeyboardState(keyState);
                var inBuffer = new byte[2];

                if (ToAscii(keyboard.KeyCode, keyboard.ScanCode, keyState, inBuffer, keyboard.Flags) == 1)
                {
                    var key = (char)inBuffer[0];
                    if (isDownCapslock ^ isDownShift && char.IsLetter(key))
                    {
                        key = char.ToUpper(key);
                    }

                    var e = new CustomKeyPressEventArgs(key);
                    KeyPress?.Invoke(this, e);

                    handled = handled || e.Handled;
                }

                #endregion
            }

            if (KeyUp != null && (wParam == MessageKeyUp || wParam == MessageSystemKeyUp))
            {
                #region Raise KeyUp

                var e = new CustomKeyEventArgs(KeyInterop.KeyFromVirtualKey(keyboard.KeyCode));

                KeyUp?.Invoke(this, e);

                handled = handled || e.Handled;

                #endregion
            }

            //If event handled in application do not handoff to other listeners.
            return(handled ? new IntPtr(1) : CallNextHookEx(_keyboardHookHandle, code, wParam, lParam));
        }
        private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.Handled = true;

            if (!(e.Command is RoutedUICommand command))
            {
                return;
            }

            switch (command.Text)
            {
            case "Copy":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.C));
                break;

            case "Cut":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.X));
                break;

            case "Paste":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.V));
                break;

            case "Select All":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.A));
                break;

            case "Undo":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Z));
                break;

            case "Redo":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Y));
                break;

            case "Backspace":
            case "DeletePreviousWord":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Back));
                break;

            case "Delete":
            case "DeleteNextWord":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Delete));
                break;

            case "MoveToLineStart":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Home));
                break;

            case "MoveToLineEnd":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.End));
                break;

            case "ToggleInsert":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Insert));
                break;

            case "MoveUpByPage":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.PageUp));
                break;

            case "MoveDownByPage":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.PageDown));
                break;

            case "MoveLeftByCharacter":
            case "MoveLeftByWord":
            case "SelectLeftByWord":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Left));
                break;

            case "MoveRightByCharacter":
            case "MoveRightByWord":
            case "SelectRightByWord":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Right));
                break;

            case "MoveDownByLine":
            case "MoveDownByParagraph":
            case "SelectDownByParagraph":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Down));
                break;

            case "MoveUpByLine":
            case "MoveUpByParagraph":
            case "SelectUpByParagraph":
                KeyDown?.Invoke(this, GetKeyEventArgs(Key.Up));
                break;
            }
        }
示例#17
0
        /// <summary>
        /// Goes through each key checking it's state compared to the last
        /// frame, triggering events if necessary. Updates the current mouse
        /// state and triggers events if necessary.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            #region Update Keyboard
            // Create the Arguments class and fill in with the modifier data
            KeyEventArgs keyEvent = new KeyEventArgs();

            foreach (Keys key in Keyboard.GetState().GetPressedKeys())
            {
                if (key == Keys.LeftAlt || key == Keys.RightAlt)
                {
                    keyEvent.Alt = true;
                }
                else if (key == Keys.LeftShift || key == Keys.RightShift)
                {
                    keyEvent.Shift = true;
                }
                else if (key == Keys.LeftControl || key == Keys.RightControl)
                {
                    keyEvent.Control = true;
                }
            }

            // Loop through our keys
            foreach (InputKey key in this.keys)
            {
                // If they are any of the modifier keys, skip them
                if (key.Key == Keys.LeftAlt || key.Key == Keys.RightAlt ||
                    key.Key == Keys.LeftShift || key.Key == Keys.RightShift ||
                    key.Key == Keys.LeftControl || key.Key == Keys.RightControl
                    )
                {
                    continue;
                }

                // Check if the key was pressed
                bool pressed = Keyboard.GetState().IsKeyDown(key.Key);

                // If it was, decrement the countdown for that key
                if (pressed)
                {
                    key.Countdown -= gameTime.ElapsedRealTime.Milliseconds;
                }

                if ((pressed) && (!key.Pressed)) // If it is pressed, but wasn't before...
                {
                    // Set some flags and invoke the KeyDown event
                    key.Pressed  = true;
                    keyEvent.Key = key.Key;

                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(keyEvent);
                    }
                }
                else if ((!pressed) && (key.Pressed)) // If it isn't pressed, but was before...
                {
                    // Set some flags, reset the countdown
                    key.Pressed   = false;
                    key.Countdown = RepeatDelay;
                    keyEvent.Key  = key.Key;

                    // Invoke the Key Up event
                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(keyEvent);
                    }
                }

                // If the Key's Countdown has zeroed out, reset it, and make
                // sure that KeyDown fires again
                if (key.Countdown < 0)
                {
                    keyEvent.Key = key.Key;

                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(keyEvent);
                    }

                    key.Countdown = RepeatRate;
                }
            }
            #endregion

            #region Update Mouse
            MouseState mouseState = Mouse.GetState();

            // Check for mouse move event
            if ((mouseState.X != this.currentMouseState.X) || (mouseState.Y != this.currentMouseState.Y))
            {
                if (MouseMove != null)
                {
                    MouseEventArgs mouseEvent = new MouseEventArgs();
                    mouseEvent.State  = mouseState;
                    mouseEvent.Button = MouseButtons.None;

                    // Cap mouse position to the window boundaries
                    mouseEvent.Position = new Point(mouseState.X, mouseState.Y);
                    if (mouseEvent.Position.X < 0)
                    {
                        mouseEvent.Position.X = 0;
                    }
                    if (mouseEvent.Position.Y < 0)
                    {
                        mouseEvent.Position.Y = 0;
                    }

                    Rectangle bounds = this.Game.Window.ClientBounds;

                    if (mouseEvent.Position.X > bounds.Width)
                    {
                        mouseEvent.Position.X = bounds.Width;
                    }
                    if (mouseEvent.Position.Y > bounds.Height)
                    {
                        mouseEvent.Position.Y = bounds.Height;
                    }


                    MouseMove.Invoke(mouseEvent);
                }
            }

            if (mouseState.LeftButton != currentMouseState.LeftButton)
            {
                if ((MouseUp != null) || (MouseDown != null))
                {
                    MouseEventArgs mouseEvent = new MouseEventArgs();
                    mouseEvent.State    = mouseState;
                    mouseEvent.Position = new Point(mouseState.X, mouseState.Y);
                    mouseEvent.Button   = MouseButtons.Left;

                    if (mouseState.LeftButton == ButtonState.Released)
                    {
                        if (MouseUp != null)
                        {
                            MouseUp.Invoke(mouseEvent);
                        }
                    }
                    else
                    {
                        if (MouseDown != null)
                        {
                            // Must request focus first, to prevent mousedown
                            // event from being swallowed up
                            if (RequestingFocus != null)
                            {
                                RequestingFocus.Invoke(mouseEvent);
                            }

                            MouseDown.Invoke(mouseEvent);
                        }
                    }
                }
            }

            if (mouseState.RightButton != currentMouseState.RightButton)
            {
                if ((MouseUp != null) || (MouseDown != null))
                {
                    MouseEventArgs mouseEvent = new MouseEventArgs();
                    mouseEvent.State    = mouseState;
                    mouseEvent.Position = new Point(mouseState.X, mouseState.Y);
                    mouseEvent.Button   = MouseButtons.Right;

                    if (mouseState.RightButton == ButtonState.Released)
                    {
                        if (MouseUp != null)
                        {
                            MouseUp.Invoke(mouseEvent);
                        }
                    }
                    else
                    {
                        if (MouseDown != null)
                        {
                            MouseDown.Invoke(mouseEvent);
                        }
                    }
                }
            }

            // Update mouse state
            this.currentMouseState = mouseState;
            #endregion

            base.Update(gameTime);
        }
示例#18
0
 protected void FireKeyDown(Keys k)
 {
     KeyDown.Invoke(this, k);
 }
 public void KeyReleased(object sender, KeyEventArgs e)
 {
     KeyDown?.Invoke(sender, e);
 }
示例#20
0
        private static IntPtr HookProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            IntPtr returnCode = KeyboardInput.CallWindowProc(KeyboardInput.prevWndProc, hWnd, msg, wParam, lParam);;

            switch (msg)
            {
            case WM_GETDLGCODE:
                returnCode = (IntPtr)DLGC_WANTALLKEYS;
                break;

            case WM_CHAR:
                CharEntered?.Invoke(null, new CharacterEventArgs((char)wParam, (int)lParam));
                break;

            case WM_KEYDOWN:
                KeyDown?.Invoke(null, new KeyEventArgs((Keys)wParam));
                break;

            case WM_KEYUP:
                KeyUp?.Invoke(null, new KeyEventArgs((Keys)wParam));
                break;

            case WM_LBUTTONDOWN:
                MouseSelection.left   = (int)lParam & 0xffff;
                MouseSelection.top    = (int)lParam >> 16;
                MouseSelection.right  = MouseSelection.left;
                MouseSelection.bottom = MouseSelection.top;
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    Acp      acp     = textBox.GetAcpByRange(MouseSelection);
                    if (acp.Start >= 0)
                    {
                        textBox.SetSelection(acp.Start, acp.End);
                        Console.WriteLine("ACPStart:{0},ACPEnd:{1}", acp.Start, acp.End);
                        Console.WriteLine("MouseDown:Left:{0},TOP:{1}RIGHT:{2}BOTTOM:{3}", MouseSelection.left, MouseSelection.top, MouseSelection.right, MouseSelection.bottom);
                        Selecting = true;
                    }
                }
                break;

            case WM_MOUSEMOVE:
                MouseSelection.right  = (int)lParam & 0xffff;
                MouseSelection.bottom = (int)lParam >> 16;
                if (Selecting && Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    RECT range = new RECT();
                    range.left   = Math.Min(MouseSelection.left, MouseSelection.right);
                    range.top    = Math.Max(MouseSelection.top, MouseSelection.bottom);
                    range.right  = Math.Max(MouseSelection.left, MouseSelection.right);
                    range.bottom = Math.Min(MouseSelection.top, MouseSelection.bottom);
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    Acp      acp     = textBox.GetAcpByRange(range);
                    if (acp.Start >= 0)
                    {
                        textBox.SetSelection(acp.Start, acp.End);
                        textBox.SetSelState(MouseSelection.left > MouseSelection.right ? SelState.SEL_AE_END : SelState.SEL_AE_START);
                        Console.WriteLine("ACPStart:{0},ACPEnd:{1}", acp.Start, acp.End);
                        Console.WriteLine("MouseMove:Left:{0},TOP:{1}RIGHT:{2}BOTTOM:{3}", MouseSelection.left, MouseSelection.top, MouseSelection.right, MouseSelection.bottom);
                    }
                }
                //handle IsMouseVisable
                returnCode = KeyboardInput.CallWindowProc(KeyboardInput.prevWndProc, hWnd, msg, wParam, lParam);
                break;

            case WM_LBUTTONUP:
                Selecting = false;
                break;

#if TSF
            case EM_GETSEL:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    Acp      acp     = textBox.GetSelection();
                    Marshal.WriteInt32(wParam, acp.Start);
                    Marshal.WriteInt32(lParam, acp.End);
                }
                break;

            case EM_SETSEL:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    textBox.SetSelection((int)wParam, (int)lParam);
                }
                break;

            case EM_REPLACESEL:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    textBox.ReplaceSelection(Marshal.PtrToStringAuto(lParam));
                }
                break;

            case TF_GETSELSTATE:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    returnCode = (IntPtr)textBox.GetSelState();
                }
                break;

            case TF_GETTEXT:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    var      text    = textBox.GetText();
                    Marshal.Copy(text.ToCharArray(), 0, wParam, Math.Min(text.Length, (int)lParam));
                }
                break;

            case TF_GETTEXTLENGTH:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    returnCode = (IntPtr)textBox.GetTextLength();
                }
                break;

            case TF_GETTEXTEXT:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    Acp      acp     = (Acp)Marshal.PtrToStructure(lParam, typeof(Acp));
                    RECT     rect    = textBox.GetTextExt(acp);
                    MapWindowPoints(Game1.game1.Window.Handle, (IntPtr)0, ref rect, 2); //to screen coord
                    Marshal.StructureToPtr(rect, wParam, false);                        //text ext

                    returnCode = (IntPtr)0;                                             //if the rect clipped
                }
                break;

            case TF_QUERYINSERT:
                if (Game1.keyboardDispatcher.Subscriber is ITextBox)
                {
                    ITextBox textBox = Game1.keyboardDispatcher.Subscriber as ITextBox;
                    Acp      acp     = (Acp)Marshal.PtrToStructure(wParam, typeof(Acp));
                    textBox.QueryInsert(acp, (uint)lParam);
                    Marshal.StructureToPtr(acp, wParam, false);
                }
                break;

            case WM_KILLFOCUS:
                Game1.tsf.TerminateComposition();
                break;
#endif
            default:
                break;
            }
            return(returnCode);
        }
示例#21
0
        private void FormOnKeyDown(object sender, KeyEventArgs e)
        {
            KeyDown?.Invoke(this, e);
            if (e.Handled)
            {
                return;
            }

            if (_capture.CaptureState != CAPTURESTATE.CAPTURING)
            {
                return;
            }

            switch (e.KeyCode)
            {
            case Keys.Down:
            {
                if (_capture.VerticalPosition > _capture.VerticalPositionMinimum)
                {
                    _capture.VerticalPosition--;
                }
                break;
            }

            case Keys.Up:
            {
                if (_capture.VerticalPosition < _capture.VerticalPositionMaximum)
                {
                    _capture.VerticalPosition++;
                }
                break;
            }

            case Keys.Right:
            {
                if (e.Shift && !e.Control)
                {
                    if (_capture.HorizontalScale < _capture.HorizontalScaleMaximum)
                    {
                        _capture.HorizontalScale++;
                    }
                }
                else if (!e.Shift && e.Control)
                {
                    if (_capture.Phase < _capture.PhaseMaximum)
                    {
                        _capture.Phase++;
                    }
                }
                else if (!e.Shift && !e.Control)
                {
                    if (_capture.HorizontalPosition > _capture.HorizontalPositionMinimum)
                    {
                        _capture.HorizontalPosition--;
                    }
                }

                break;
            }

            case Keys.Left:
            {
                if (e.Shift && !e.Control)
                {
                    if (_capture.HorizontalScale > _capture.HorizontalScaleMinimum)
                    {
                        _capture.HorizontalScale--;
                    }
                }
                else if (!e.Shift && e.Control)
                {
                    if (_capture.Phase > _capture.PhaseMinimum)
                    {
                        _capture.Phase--;
                    }
                }
                else if (!e.Shift && !e.Control)
                {
                    if (_capture.HorizontalPosition < _capture.HorizontalPositionMaximum)
                    {
                        _capture.HorizontalPosition++;
                    }
                }

                break;
            }

            case Keys.Oemplus:
            {
                _scale++;
                UpdateFormGeometry(false);
                break;
            }

            case Keys.OemMinus:
            {
                if (_scale > 1)
                {
                    _scale--;
                }
                UpdateFormGeometry(false);
                break;
            }
            }
        }
示例#22
0
 protected virtual void OnKeyDown(KeyEventArgs e)
 {
     KeyDown?.Invoke(this, e);
 }
示例#23
0
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////
        private void UpdateKeys(KeyboardState state, GameTime gameTime)
        {
#if (!XBOX && !XBOX_FAKE)
            KeyEventArgs e = new KeyEventArgs();

            //e.Caps = (((ushort)NativeMethods.GetKeyState(0x14)) & 0xffff) != 0;

            foreach (Keys key in state.GetPressedKeys())
            {
                if (key == Keys.LeftAlt || key == Keys.RightAlt)
                {
                    e.Alt = true;
                }
                else if (key == Keys.LeftShift || key == Keys.RightShift)
                {
                    e.Shift = true;
                }
                else if (key == Keys.LeftControl || key == Keys.RightControl)
                {
                    e.Control = true;
                }
            }

            foreach (InputKey key in keys)
            {
                if (key.Key == Keys.LeftAlt || key.Key == Keys.RightAlt ||
                    key.Key == Keys.LeftShift || key.Key == Keys.RightShift ||
                    key.Key == Keys.LeftControl || key.Key == Keys.RightControl)
                {
                    continue;
                }

                bool pressed = state.IsKeyDown(key.Key);

                double ms = gameTime.ElapsedGameTime.TotalMilliseconds;
                if (pressed)
                {
                    key.Countdown -= ms;
                }

                if ((pressed) && (!key.Pressed))
                {
                    key.Pressed = true;
                    e.Key       = key.Key;

                    if (KeyDown != null)
                    {
                        KeyDown.Invoke(this, e);
                    }
                    if (KeyPress != null)
                    {
                        KeyPress.Invoke(this, e);
                    }
                }
                else if ((!pressed) && (key.Pressed))
                {
                    key.Pressed   = false;
                    key.Countdown = RepeatDelay;
                    e.Key         = key.Key;

                    if (KeyUp != null)
                    {
                        KeyUp.Invoke(this, e);
                    }
                }
                else if (key.Pressed && key.Countdown < 0)
                {
                    key.Countdown = RepeatRate;
                    e.Key         = key.Key;

                    if (KeyPress != null)
                    {
                        KeyPress.Invoke(this, e);
                    }
                }
            }
#endif
        }
示例#24
0
 public void onKeyDown(KeyDown kd)
 {
     EKeyDown = kd;
 }
示例#25
0
 /// <summary>
 /// Вызывает событие нажатия клавиши
 /// </summary>
 /// <param name="parE">Параметры события</param>
 protected virtual void CallKeyDown(KeyDownEventArgs parE)
 {
     KeyDown?.Invoke(this, parE);
 }
示例#26
0
 protected virtual void OnKeyDownPressed(object sender, Key args)
 {
     KeyDown?.Invoke(this, args);
 }
示例#27
0
        private void RegisterWindowCallbacks()
        {
            if (_callbackRegistered)
            {
                return;
            }
            _callbackRegistered = true;

            // [NOTE]
            // Do not register callback to GLFW as lambda or method. (That cannot work)
            // Put delegate on a field and register it to GLFW.

            _posCallback = (wnd, x, y) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                _location = new Vector2i(x, y);
                Move?.Invoke(this, new WindowPositionEventArgs(x, y));
            };
            GLFW.SetWindowPosCallback(_window, _posCallback);

            _sizeCallback = (wnd, width, height) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                GLFW.GetWindowFrameSize(_window, out var left, out var top, out var right, out var bottom);
                _clientSize = new Vector2i(width, height);
                _size       = new Vector2i(_clientSize.X + left + right, _clientSize.Y + top + bottom);
                Resize?.Invoke(this, new ResizeEventArgs(width, height));
            };
            GLFW.SetWindowSizeCallback(_window, _sizeCallback);

            _frameBufferSizeCallback = (wnd, width, height) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                _frameBufferSize = new Vector2i(width, height);
                FrameBufferSizeChanged?.Invoke(this, _frameBufferSize);
            };
            GLFW.SetFramebufferSizeCallback(_window, _frameBufferSizeCallback);

            _closeCallback = wnd =>
            {
                if (wnd != _window)
                {
                    return;
                }
                var cancel = false;
                var e      = new CancelEventArgs(&cancel);
                Closing?.Invoke(this, e);
                if (e.Cancel)
                {
                    GLFW.SetWindowShouldClose(_window, false);
                }
            };
            GLFW.SetWindowCloseCallback(_window, _closeCallback);


            _iconifyCallback = (wnd, minimized) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                Minimized?.Invoke(this, new MinimizedEventArgs(minimized));
            };
            GLFW.SetWindowIconifyCallback(_window, _iconifyCallback);

            _focusCallback = (wnd, focused) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                FocusedChanged?.Invoke(this, new FocusedChangedEventArgs(focused));
            };
            GLFW.SetWindowFocusCallback(_window, _focusCallback);

            _charCallback = (wnd, unicode) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                CharInput?.Invoke(this, new CharInputEventArgs(unicode));
            };
            GLFW.SetCharCallback(_window, _charCallback);

            _keyCallback = (wnd, glfwKey, scanCode, action, glfwMods) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                var e = new KeyboardKeyEventArgs(glfwKey, scanCode, glfwMods, action == GlfwInputAction.Repeat);

                if (action == GlfwInputAction.Release)
                {
                    KeyUp?.Invoke(this, e);
                }
                else
                {
                    KeyDown?.Invoke(this, e);
                }
            };
            GLFW.SetKeyCallback(_window, _keyCallback);

            _cursorEnterCallback = (wnd, entered) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                if (entered)
                {
                    MouseEnter?.Invoke(this);
                }
                else
                {
                    MouseLeave?.Invoke(this);
                }
            };
            GLFW.SetCursorEnterCallback(_window, _cursorEnterCallback);

            _mouseButtonCallback = (wnd, button, action, mods) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                var e = new MouseButtonEventArgs(button, action, mods);

                if (action == GlfwInputAction.Release)
                {
                    MouseUp?.Invoke(this, e);
                }
                else
                {
                    MouseDown?.Invoke(this, e);
                }
            };
            GLFW.SetMouseButtonCallback(_window, _mouseButtonCallback);

            _cursorPosCallback = (wnd, posX, posY) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                var e = new MouseMoveEventArgs(new Vector2((int)posX, (int)posY));
                MouseMove?.Invoke(this, e);
            };
            GLFW.SetCursorPosCallback(_window, _cursorPosCallback);

            _scrollCallback = (wnd, offsetX, offsetY) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                var e = new MouseWheelEventArgs((float)offsetX, (float)offsetY);
                MouseWheel?.Invoke(this, e);
            };
            GLFW.SetScrollCallback(_window, _scrollCallback);

            _dropCallback = (wnd, count, paths) =>
            {
                if (wnd != _window)
                {
                    return;
                }
                var e = new FileDropEventArgs(count, paths);
                FileDrop?.Invoke(this, e);
            };
            GLFW.SetDropCallback(_window, _dropCallback);

            _joystickCallback = (joystick, state) =>
            {
                var e = new JoystickConnectionEventArgs(joystick, state == GlfwConnectedState.Connected);
                JoystickConnectionChanged?.Invoke(this, e);
            };
            GLFW.SetJoystickCallback(_joystickCallback);

            _monitorCallback = (monitor, state) =>
            {
                var e = new MonitorConnectionEventArgs(monitor, state == GlfwConnectedState.Connected);
                MonitorConnectionChanged?.Invoke(this, e);
            };
            GLFW.SetMonitorCallback(_monitorCallback);

            _refreshCallback = wnd =>
            {
                if (wnd != _window)
                {
                    return;
                }
                Refresh?.Invoke(this);
            };
            GLFW.SetWindowRefreshCallback(_window, _refreshCallback);
        }
示例#28
0
 private void Button_KeyDown(object sender, KeyEventArgs e)
 {
     blnPressed = (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space);
     Invalidate();
     KeyDown?.Invoke(this, e);
 }
示例#29
0
        /// <summary>
        /// A callback function which will be called every time a keyboard activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            //Indicates if any of underlaing events set e.Handled flag
            var handled = false;

            //If was Ok and someone listens to events
            if (nCode < 0 || KeyDown == null && KeyUp == null && KeyPress == null)
            {
                return(CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
            }

            //Read structure KeyboardHookStruct at lParam
            var myKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

            if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
            {
                #region Raise KeyDown

                var isDownShift    = (GetKeyState(VK_SHIFT) & 0x80) == 0x80;
                var isDownCapslock = GetKeyState(VK_CAPITAL) != 0;

                var e = new CustomKeyEventArgs(KeyInterop.KeyFromVirtualKey(myKeyboardHookStruct.vkCode), isDownCapslock ^ isDownShift);

                KeyDown?.Invoke(this, e);

                handled = e.Handled;

                #endregion
            }

            if (KeyPress != null && wParam == WM_KEYDOWN)
            {
                #region Raise KeyPress

                var isDownShift    = (GetKeyState(VK_SHIFT) & 0x80) == 0x80;
                var isDownCapslock = GetKeyState(VK_CAPITAL) != 0;

                var keyState = new byte[256];
                GetKeyboardState(keyState);
                var inBuffer = new byte[2];

                if (ToAscii(myKeyboardHookStruct.vkCode, myKeyboardHookStruct.scanCode, keyState, inBuffer, myKeyboardHookStruct.flags) == 1)
                {
                    var key = (char)inBuffer[0];
                    if (isDownCapslock ^ isDownShift && char.IsLetter(key))
                    {
                        key = char.ToUpper(key);
                    }

                    var e = new CustomKeyPressEventArgs(key);
                    KeyPress?.Invoke(this, e);

                    handled = handled || e.Handled;
                }

                #endregion
            }

            if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
            {
                #region Raise KeyUp

                var e = new CustomKeyEventArgs(KeyInterop.KeyFromVirtualKey(myKeyboardHookStruct.vkCode));

                KeyUp?.Invoke(this, e);

                handled = handled || e.Handled;

                #endregion
            }

            //If event handled in application do not handoff to other listeners
            return(handled ? 1 : CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
        }
示例#30
0
 internal void OnKeyDown(KeyboardEventArgs e) => KeyDown?.Invoke(e);
示例#31
0
 /// <summary>
 /// Raises the KeyDown event. Override this method to add code to handle when a key is pressed
 /// </summary>
 /// <param name="e">InputEventCKB for the KeyDown event</param>
 private void invokeKeyDown(InputEventKeyboard e)
 {
     KeyDown?.Invoke(e);
 }