Beispiel #1
0
        internal void HandleEvents()
        {
            var elapsed = (float)AppTime.Elapsed.TotalSeconds;

            foreach (var keys in _pressed)
            {
                float diff = elapsed - _lastPress[(int)keys];
                KeyHeld?.Invoke(this, new KeyEventData(KeyEventType.Held, keys, ModifierMask, diff));
            }
        }
Beispiel #2
0
        /// <summary>
        /// A method that is attached to the KeyPressed event; used to invoke other events, such as KeyHeld, KeyJustPressed and KeyJustReleased.
        /// </summary>
        private static void OnKeyPress(object sender, KeyEventArgs e)
        {
            // KeyHeld event:
            if (IsKeyHeld(e.Key))
            {
                KeyHeld?.Invoke(null, e);
            }

            // KeyJustPressed event:
            if (IsKeyJustPressed(e.Key))
            {
                KeyJustPressed?.Invoke(null, e);
            }

            // DOESN'T WORK
            // KeyJustReleased event:
            if (IsKeyJustReleased(e.Key))
            {
                KeyJustReleased?.Invoke(null, e);
            }
        }
Beispiel #3
0
        // The method responsible for handling the update
        private static void Update(object s, EventArgs e)
        {
            KeyboardState New = Keyboard.GetState();

            Keys[] OldDown = Old.GetPressedKeys();
            Keys[] Down    = New.GetPressedKeys().Where(a => !Old.IsKeyDown(a)).ToArray();
            Keys[] Up      = OldDown.Where(a => !New.IsKeyDown(a)).ToArray();
            Keys[] Held    = OldDown.Where(a => New.IsKeyDown(a)).ToArray();
            foreach (Keys key in Down)
            {
                KeyDown?.Invoke(key);
                KeyPress?.Invoke(key);
                // int[]{ticks,tickRate}
                Counter.Add(key, new int[2] {
                    30, 30
                });
            }
            foreach (Keys key in Up)
            {
                Counter.Remove(key);
                KeyUp?.Invoke(key);
            }
            foreach (Keys key in Held)
            {
                Counter[key][0]--;
                if (Counter[key][0] > 0)
                {
                    continue;
                }
                Counter[key][0] = Counter[key][1];
                if (Counter[key][1] > 15)
                {
                    Counter[key][1]--;
                }
                KeyHeld?.Invoke(key);
                KeyPress?.Invoke(key);
            }
            Old = New;
        }
        public static void InputLoop()
        {
            uint        toRead            = 128;
            var         records           = new INPUT_RECORD[toRead];
            int         prevWidth         = MyConsole.Width;
            int         prevHeight        = MyConsole.Height;
            MouseButton prevMouseState    = MouseButton.None;
            COORD       prevMouseLocation = new COORD();

            while (!MyConsole.Exiting)
            {
                WinApi.ReadConsoleInput(inputHandle, records, toRead, out uint recordLen);

                for (int i = 0; i < recordLen; i++)
                {
                    var record = records[i];

                    switch (record.EventType)
                    {
                    case EventType.Mouse:
                    {
                        var mouseEvent = record.Event.MouseEvent;
                        var button     = mouseEvent.ButtonState;
                        var flags      = mouseEvent.EventFlags;
                        var location   = mouseEvent.MousePosition;

                        bool mousePressed  = prevMouseState == MouseButton.None && button != MouseButton.None;
                        bool mouseReleased = prevMouseState != MouseButton.None && button == MouseButton.None;
                        bool mouseHeld     = prevMouseState != MouseButton.None && button != MouseButton.None;

                        var args = new MouseEventArgs
                        {
                            Button          = button,
                            Location        = location,
                            ControlKeyState = mouseEvent.ControlKeyState
                        };

                        bool sameLocation = location.Equals(prevMouseLocation);

                        if (mousePressed && flags.HasFlag(MouseState.DoubleClick))
                        {
                            MouseDoubleClick?.Invoke(null, args);
                        }
                        else if (mousePressed)
                        {
                            MousePressed?.Invoke(null, args);
                        }
                        else if (mouseReleased)
                        {
                            MouseReleased?.Invoke(null, args);
                        }
                        else if (mouseHeld && flags.HasFlag(MouseState.Moved) && !sameLocation)
                        {
                            MouseDragged?.Invoke(null, args);
                        }
                        else if (flags.HasFlag(MouseState.Moved) && !sameLocation)
                        {
                            MouseMoved?.Invoke(null, args);
                        }

                        prevMouseState    = button;
                        prevMouseLocation = location;
                    }
                    break;

                    case EventType.Key:
                    {
                        var keyEvent = record.Event.KeyEvent;

                        var eventArgs = new KeyEventArgs
                        {
                            Key             = (ConsoleKey)keyEvent.VirtualKeyCode,
                            ControlKeyState = keyEvent.ControlKeyState
                        };

                        bool currState = keyEvent.KeyDown;
                        bool prevState = keyStates[keyEvent.VirtualKeyCode];

                        if (currState && !prevState)
                        {
                            KeyPressed?.Invoke(eventArgs);
                        }
                        else if (prevState && !currState)
                        {
                            KeyReleased?.Invoke(eventArgs);
                        }
                        else if (prevState && currState)
                        {
                            KeyHeld?.Invoke(eventArgs);
                        }

                        keyStates[keyEvent.VirtualKeyCode] = keyEvent.KeyDown;
                    }
                    break;

                    case EventType.Resize:
                    {
                        var clientSize = MyConsole.GetClientSize();
                        var fontSize   = MyConsole.GetFontSize();
                        int w          = clientSize.X / fontSize.X;
                        int h          = clientSize.Y / fontSize.Y;

                        if (prevWidth != w || prevHeight != h)
                        {
                            MyConsole.SetSize(w, h);
                            MyConsole.HideCursor();
                            Drawing.ConsoleRenderer.Resize(w, h);
                            Resized?.Invoke(new ResizedEventArgs
                                {
                                    Width  = w,
                                    Height = h
                                });
                            prevWidth  = w;
                            prevHeight = h;
                        }
                    }
                    break;

                    case EventType.Menu:
                    {
                        var id = record.Event.MenuEvent.dwCommandId;
                        Debug.WriteLine(id);
                    }
                    break;

                    case EventType.Focus:
                    {
                        var focused = record.Event.FocusEvent.bSetFocus;
                    }
                    break;

                    default:
                        Debug.WriteLine("Unhandled event: " + record.EventType);
                        break;
                    }
                }
            }
        }
 private void FireKeyHeld(Keys key)
 {
     KeyHeld?.Invoke(this, new KeyHeldEventArgs(key));
 }
Beispiel #6
0
 public void Handle(KeyHeld message)
 {
     HandleKey(message.Key);
 }
Beispiel #7
0
        public void Update()
        {
            var state        = Microsoft.Xna.Framework.Input.Keyboard.GetState();
            var curr_pressed = state.GetPressedKeys();

            // get pressed keys
            // check if previously pressed keys are now not pressed - send key up events
            // check if previously pressed keys still pressed - send key held events
            for (int p = 0; p < mPrevPressedKeys.Count; ++p)
            {
                bool found = false;

                for (int c = 0; c < curr_pressed.Length; ++c)
                {
                    if (curr_pressed[c] == mPrevPressedKeys[p])
                    {
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    KeyHeld msg = new KeyHeld();
                    msg.mKey = mPrevPressedKeys[p];
                    mBroadcaster_KeyHeld.Post(msg);
                }
                else
                {
                    KeyUp msg = new KeyUp();
                    msg.mKey = mPrevPressedKeys[p];
                    mBroadcaster_KeyUp.Post(msg);
                }
            }

            // check if previously not pressed keys are now pressed - send key down events
            for (int c = 0; c < curr_pressed.Length; ++c)
            {
                bool found = false;

                for (int p = 0; p < mPrevPressedKeys.Count; ++p)
                {
                    if (mPrevPressedKeys[p] == curr_pressed[c])
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    KeyDown msg = new KeyDown();
                    msg.mKey = curr_pressed[c];
                    mBroadcaster_KeyDown.Post(msg);
                }
            }

            // update keys prev pressed
            mPrevPressedKeys.Clear();

            for (int c = 0; c < curr_pressed.Length; ++c)
            {
                mPrevPressedKeys.Add(curr_pressed[c]);
            }
        }