Beispiel #1
0
 public void OnKeyDown(PreviewKeyDownEventArgs e)
 {
     Handler.KeyInPutHandler(this, e.KeyCode, KeyHandlerAction.Down);
     if (!PressedKeys.Contains(e.KeyData))
     {
         PressedKeys.Add(e.KeyData);
     }
 }
Beispiel #2
0
        private void keyDown(object sender, KeyEventArgs e)
        {
            Key tkKey = getTkKeyFrom(e.KeyCode);

            if (tkKey != Key.Unknown && !PressedKeys.Contains(tkKey))
            {
                PressedKeys.Add(tkKey);
            }
        }
Beispiel #3
0
        private static void UpdateInputs(float deltaTime)
        {
            // Set old states.
            oldScrollValue   = scrollValue;
            oldMouseState    = MouseState;
            oldKeyboardState = KeyboardState;

            // Get current keyboard and mouse states.
            KeyboardState = Keyboard.GetState();
            MouseState    = Mouse.GetState();
            PressedChars.AddRange(oldPressedChars);

            // Check key state capacities and move the key states into arrays.
            OldKeys = oldKeyboardState.GetPressedKeys();
            NewKeys = KeyboardState.GetPressedKeys();
            for (int i = 0; i < NewKeys.Length; i++)
            {
                if (!OldKeys.Contains(NewKeys[i]))
                {
                    PressedKeys.Add(NewKeys[i]);
                }
            }

            // Set mouse properties.
            LeftMouseDown  = MouseState.LeftButton == ButtonState.Pressed;
            RightMouseDown = MouseState.RightButton == ButtonState.Pressed;
            MouseDown      = LeftMouseDown || RightMouseDown;

            LeftMouseClicked  = oldMouseState.LeftButton == ButtonState.Released && LeftMouseDown;
            RightMouseClicked = oldMouseState.RightButton == ButtonState.Released && RightMouseDown;
            MouseClicked      = LeftMouseClicked || RightMouseClicked;

            LeftMouseReleased  = oldMouseState.LeftButton == ButtonState.Pressed && !LeftMouseDown;
            RightMouseReleased = oldMouseState.RightButton == ButtonState.Pressed && !RightMouseDown;
            MouseReleased      = LeftMouseReleased || RightMouseReleased;

            // Set the scroll wheel state.
            scrollValue = MouseState.ScrollWheelValue;
            if (scrollValue > oldScrollValue)
            {
                MouseScrollValue = 1;
            }
            else if (scrollValue < oldScrollValue)
            {
                MouseScrollValue = -1;
            }
            else
            {
                MouseScrollValue = 0;
            }

            for (int i = 0; i < controllers.Length; i++)
            {
                controllers[i].Update(deltaTime);
            }
        }
Beispiel #4
0
 /// <summary>
 /// Simulate key press
 /// </summary>
 public void KeyPress(Keys key)
 {
     if (!PressedKeys.Contains(key))
     {
         PressedKeys.Add(key);
     }
     if (!SimulatedKeys.Contains(key))
     {
         SimulatedKeys.Add(key);
     }
 }
Beispiel #5
0
        private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            // https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

            // Remote Play not found
            if (CurrentProcess == null)
            {
                return;
            }

            // Check for active window
            var activeWindow = GetForegroundWindow();

            if (activeWindow != IntPtr.Zero)
            {
                if (activeWindow != CurrentProcess.MainWindowHandle)
                {
                    return;
                }
            }

            int  vk  = e.KeyboardData.VirtualCode;
            Keys key = (Keys)vk;

            //System.Diagnostics.Debug.WriteLine("KEY: " + vk);

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown)
            {
                //System.Diagnostics.Debug.WriteLine("KEY DOWN");

                if (!PressedKeys.ContainsKey(key))
                {
                    PressedKeys.Add(key, true);
                }

                e.Handled = true;
            }
            else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp)
            {
                //System.Diagnostics.Debug.WriteLine("KEY UP");

                if (PressedKeys.ContainsKey(key))
                {
                    PressedKeys.Remove(key);
                }

                e.Handled = true;
            }
        }
        public override void UpdateInput(bool isActive)
        {
            PressedKeys.Clear();

            if (state.IsAnyKeyDown)
            {
                foreach (Key k in Enum.GetValues(typeof(Key)))
                {
                    if (state.IsKeyDown(k))
                    {
                        PressedKeys.Add(k);
                    }
                }
            }
        }
Beispiel #7
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            ReleasedKeys.Clear();
            ClickedKeys.Clear();

            KeyboardState state = Keyboard.GetState();

            foreach (IPressedKey pressedKey in new List <IPressedKey>(PressedKeys))
            {
                bool isPressed = false;
                foreach (Keys key in state.GetPressedKeys())
                {
                    if (key == pressedKey.Key)
                    {
                        isPressed = true;
                    }
                }

                if (!isPressed)
                {
                    PressedKeys.Remove(pressedKey);
                    ReleasedKeys.Add(pressedKey.Key);
                }
            }

            foreach (Keys key in state.GetPressedKeys())
            {
                bool isAlreadyPressed = false;

                foreach (IPressedKey pressedKey in PressedKeys)
                {
                    if (key == pressedKey.Key)
                    {
                        isAlreadyPressed = true;
                    }
                }

                if (!isAlreadyPressed)
                {
                    PressedKeys.Add(new PressedKey(key, gameTime.TotalGameTime.TotalSeconds));
                    ClickedKeys.Add(key);
                }
            }
        }
Beispiel #8
0
        protected override void OnToolKeyDown(MapViewKeyEventArgs k)
        {
            _msg.VerboseDebug("OnToolKeyDown");

            try
            {
                PressedKeys.Add(k.Key);

                if (IsModifierKey(k.Key) || HandledKeys.Contains(k.Key))
                {
                    k.Handled = true;
                }

                if (k.Key == _keyShowOptionsPane)
                {
                    ShowOptionsPane();
                }

                // Cancel outside a queued task otherwise the current task that blocks the queue
                // cannot be cancelled.
                if (k.Key == Key.Escape)
                {
                    HandleEscape();
                }

                QueuedTaskUtils.Run(
                    delegate
                {
                    if (IsShiftKey(k.Key) &&
                        SelectionCursorShift != null && IsInSelectionPhase())
                    {
                        SetCursor(SelectionCursorShift);
                    }

                    OnKeyDownCore(k);

                    return(true);
                });
            }
            catch (Exception e)
            {
                HandleError($"Error in tool key down ({Caption}): {e.Message}", e, true);
            }
        }
        public virtual void Update()
        {
            PressedKeys.Clear();
            ReleasedKeys.Clear();

            foreach (KeyEventArgs keyEvent in keyEvents)
            {
                if (DownKeys.Contains(keyEvent.Key))
                {
                    PressedKeys.Add(keyEvent.Key);
                }
                else
                {
                    ReleasedKeys.Add(keyEvent.Key);
                }
            }

            keyEvents.Clear();
        }
Beispiel #10
0
        public void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            // https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

            if (!CheckFocusedWindow())
            {
                return;
            }

            int  vk  = e.KeyboardData.VirtualCode;
            Keys key = (Keys)vk;

            // Key down
            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown)
            {
                if (!PressedKeys.ContainsKey(key))
                {
                    PressedKeys.Add(key, true);
                    ExecuteActionsByKey(PressedKeys.Keys.ToList());
                }

                e.Handled = true;
            }
            // Key up
            else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp)
            {
                if (PressedKeys.ContainsKey(key))
                {
                    PressedKeys.Remove(key);
                    ExecuteActionsByKey(PressedKeys.Keys.ToList());
                }

                e.Handled = true;
            }

            // Reset state
            if (!IsKeyDown())
            {
                CurrentState = null;
            }
        }
Beispiel #11
0
 internal void OnKeyDown(object sender, KeyEventArgs e)
 {
     PressedKeys.Add(e.KeyCode);
     HeldKeys.Add(e.KeyCode);
 }
Beispiel #12
0
        /// <summary>
        /// Has to be called every Update to ensure correct calling
        /// </summary>
        public void Update(GameTime gameTime)
        {
            // Shift pressed to lastPressed
            LastPressedKeys = new List <Keys>(PressedKeys);
            PressedKeys.Clear();

            KeyboardState state = Keyboard.GetState();

            PressedKeys.AddRange(state.GetPressedKeys());

            // Add simulated keys
            foreach (Keys key in SimulatedKeys)
            {
                if (!PressedKeys.Contains(key))
                {
                    PressedKeys.Add(key);
                }
            }

            // Get pressed time for gmkeyboard.string
            // Remove released keys
            foreach (Keys key in PressTime.Keys.ToList())
            {
                if (!PressedKeys.Contains(key))
                {
                    PressTime.Remove(key);
                }
            }
            // Add newly pressed keys to pressTime
            foreach (Keys key in PressedKeys)
            {
                if (!PressTime.ContainsKey(key))
                {
                    PressTime.Add(key, -gameTime.ElapsedGameTime.TotalMilliseconds);
                }
            }

            Dictionary <Keys, double> lastPressTime = new Dictionary <Keys, double>(PressTime);

            TriggeredKeys.Clear();
            // Handle KeyboardString
            foreach (Keys key in PressTime.Keys.ToList())
            {
                // Don't add key to string if it isn't allowed to
                if (Settings.AddSimulatedKeysToKeyboardString == false)
                {
                    if (SimulatedKeys.Contains(key))
                    {
                        break;
                    }
                }

                PressTime[key] += gameTime.ElapsedGameTime.TotalMilliseconds;
                bool shouldFire = false;

                // Fire if key is just pressed
                if (PressTime[key] == 0)
                {
                    shouldFire = true;
                }

                // Check if it should refire because key is hold
                if (PressTime[key] >= Settings.ReFireDelay)
                {
                    int maxTime = Settings.ReFireDelay + Settings.ReFireInterval * 20;
                    if (PressTime[key] > maxTime)
                    {
                        PressTime[key]     -= Settings.ReFireInterval * 20;
                        lastPressTime[key] -= Settings.ReFireInterval * 20;
                    }
                    for (int t = Settings.ReFireDelay; t < maxTime; t += Settings.ReFireInterval)
                    {
                        if (PressTime[key] > t && lastPressTime[key] < t)
                        {
                            shouldFire = true;
                        }
                    }
                }

                if (shouldFire)
                {
                    TriggeredKeys.Add(key);
                    // s = shift pressed?
                    bool   s         = Check(Keys.LeftShift) | Check(Keys.RightShift);
                    string keyString = key.ToString();
                    if (Alphabet.Contains(key))
                    {
                        s       = state.CapsLock ? !s : s; // Invert shift if CapsLock is on
                        String += s ? keyString : keyString.ToLower();
                    }
                    else
                    {
                        bool nl = state.NumLock;
                        switch (key)
                        {
                        case Keys.Space: String += " "; break;

                        case Keys.Back: if (String.Length > 0)
                            {
                                String = String.Substring(0, String.Length - 1);
                            }
                            break;

                        case Keys.D1: String += s ? "!" : "1"; break;

                        case Keys.D2: String += s ? "@" : "2"; break;

                        case Keys.D3: String += s ? "#" : "3"; break;

                        case Keys.D4: String += s ? "$" : "4"; break;

                        case Keys.D5: String += s ? "%" : "5"; break;

                        case Keys.D6: String += s ? "^" : "6"; break;

                        case Keys.D7: String += s ? "&" : "7"; break;

                        case Keys.D8: String += s ? "*" : "8"; break;

                        case Keys.D9: String += s ? "(" : "9"; break;

                        case Keys.D0: String += s ? ")" : "0"; break;

                        case Keys.OemComma: String += s ? "<" : ","; break;

                        case Keys.OemPeriod: String += s ? ">" : "."; break;

                        case Keys.OemQuestion: String += s ? "?" : "/"; break;

                        case Keys.OemSemicolon: String += s ? ":" : ";"; break;

                        case Keys.OemQuotes: String += s ? "\"" : "'"; break;

                        case Keys.OemPipe: String += s ? "|" : "\\"; break;

                        case Keys.OemMinus: String += s ? "_" : "-"; break;

                        case Keys.OemPlus: String += s ? "+" : "="; break;

                        case Keys.OemOpenBrackets: String += s ? "{" : "["; break;

                        case Keys.OemCloseBrackets: String += s ? "}" : "]"; break;

                        case Keys.OemTilde: String += s ? "~" : "`"; break;

                        case Keys.NumPad0: String += nl ? "0" : ""; break;

                        case Keys.NumPad1: String += nl ? "1" : ""; break;

                        case Keys.NumPad2: String += nl ? "2" : ""; break;

                        case Keys.NumPad3: String += nl ? "3" : ""; break;

                        case Keys.NumPad4: String += nl ? "4" : ""; break;

                        case Keys.NumPad5: String += nl ? "5" : ""; break;

                        case Keys.NumPad6: String += nl ? "6" : ""; break;

                        case Keys.NumPad7: String += nl ? "7" : ""; break;

                        case Keys.NumPad8: String += nl ? "8" : ""; break;

                        case Keys.NumPad9: String += nl ? "9" : ""; break;

                        case Keys.Multiply: String += "*"; break;

                        case Keys.Divide: String += "/"; break;

                        case Keys.Add: String += "+"; break;

                        case Keys.Subtract: String += "-"; break;

                        case Keys.Decimal: String += "."; break;

                        case Keys.Enter: String += Settings.ParseEnter ? "\n" : ""; break;

                        case Keys.Tab: String += Settings.ParseTab ? "\t" : ""; break;

                        default: break;
                        }
                    }

                    // Limit Keyboard.String length
                    if (String.Length > Settings.StoreLength)
                    {
                        String = String.Substring(String.Length - Settings.StoreLength);
                    }
                }
            }
        }
Beispiel #13
0
        static public void InputUpdater()
        {
            var running            = true;
            var threadLimiterWatch = new Stopwatch();

            threadLimiterWatch.Start();

            while (running)
            {
                Thread.Yield();
                if (threadLimiterWatch.ElapsedMilliseconds >= 1)
                {
                    threadLimiterWatch.Restart();

                    try
                    {
                        KeyboardState = Keyboard.GetState();
                        MouseState    = Mouse.GetState();

                        if (KeyboardState.GetPressedKeys().Count() > 0)
                        {
                            foreach (Keys key in KeyboardState.GetPressedKeys())
                            {
                                if (!PressedKeys.Contains(key))
                                {
                                    PressActions.Enqueue(new KeyValuePair <double, Keys>(AudioManager.GetTime(), key));
                                    PressedKeys.Add(key);

                                    if (key == Keys.CapsLock)
                                    {
                                        CapsLock = !CapsLock;
                                        Caps     = CapsLock;
                                    }

                                    if (key == Keys.LeftShift || key == Keys.RightShift)
                                    {
                                        Caps = !CapsLock;
                                    }
                                }
                            }
                        }

                        for (int i = 0; i < PressedKeys.Count; i++)
                        {
                            Keys key = PressedKeys[i];

                            if (!KeyboardState.IsKeyDown(key))
                            {
                                // Used if LN handling
                                //keyboardReleases.Enqueue(new KeyValuePair<double, Keys>(time, key));
                                PressedKeys.RemoveAt(i);
                                i--;


                                if (key == Keys.LeftShift || key == Keys.RightShift)
                                {
                                    Caps = CapsLock;
                                }
                            }
                        }

                        if (LastMouseClick.Value == 0 && MouseState.LeftButton == ButtonState.Pressed)
                        {
                            LastMouseClick.Key   = MouseState;
                            LastMouseClick.Value = 1;
                        }
                        else if (LastMouseClick.Value == 1 && MouseState.LeftButton == ButtonState.Released)
                        {
                            LastMouseClick.Key   = MouseState;
                            LastMouseClick.Value = 2;
                        }
                    }
                    catch { }
                }
            }
        }