Search Results for

    Show / Hide Table of Contents

    Keyboard

    The keyboard is the most common input device on desktop platforms. You can access the keyboard state by using KeyboardDispatcher.

    KeyboardDispatcher

    The KeyboardDispatcher is a class used to track keyboard key events.

    public abstract class KeyboardDispatcher
    {
        public event EventHandler<KeyCharEventArgs> KeyChar;
        public event EventHandler<KeyEventArgs> KeyDown;
        public event EventHandler<KeyEventArgs> KeyUp;
    
        public bool IsKeyDown(Keys key);
        public ButtonState ReadKeyState(Keys key);
    }
    

    Events

    Event Description
    KeyChar Occurs when a key is pressed and a character is generated. The KeyChar event is useful for text input.
    KeyDown and KeyUp These events are available to track pressed keyboard keys, but it is recommended to use the IsKeyDown and ReadKeyState methods.
    • IsKeyDown: Gets a value indicating whether the current state of a keyboard key is Pressing or Pressed.
    • ReadKeyState: Gets the current state of a keyboard key.

    Using KeyboardDispatcher

    The KeyboardDispatcher can be found within the Display or Surface objects. The following sample code demonstrates how to access the keyboard dispatcher from a Component or Service.

    [BindService]
    protected GraphicsPresenter graphicsPresenter;
    
    protected override void Update(TimeSpan time)
    {
        KeyboardDispatcher keyboardDispatcher = this.graphicsPresenter.FocusedDisplay?.KeyboardDispatcher;
    
        if (keyboardDispatcher?.ReadKeyState(Keys.A) == ButtonState.Pressing)
        {
            // Do something
        }
    }
    
    In this article
    Back to top
    Generated by DocFX