Keyboard API Reference

This document provides a reference for the Universal Windows Platform (UWP) keyboard input APIs, allowing developers to capture and process keyboard events within their applications.

Core Concepts

The UWP keyboard input model is built around handling events that are dispatched when keys are pressed, released, or repeated. This allows for responsive and intuitive user interactions.

Key Events

Key Information

When a keyboard event occurs, you receive an object containing critical information such as:

Classes and Structures

Windows.UI.Core.CoreVirtualKey

Represents the virtual key code for a physical key on the keyboard.

Member Description
A The 'A' key.
B The 'B' key.
Enter The Enter key.
Space The Spacebar key.
Shift The Shift key.
Control The Control key.
Alt The Alt key.
Escape The Escape key.
Tab The Tab key.
LeftArrow The Left Arrow key.
RightArrow The Right Arrow key.
UpArrow The Up Arrow key.
DownArrow The Down Arrow key.

Windows.UI.Core.KeyEventArgs

Provides data for keyboard events.

Property Type Description
VirtualKey Windows.System.VirtualKey Gets the virtual key code associated with the key event.
KeyStatus Windows.UI.Core.CorePhysicalKeyStatus Gets the status of the physical key, including repeat count and whether modifier keys were pressed.

Windows.UI.Core.CorePhysicalKeyStatus

Contains information about the state of a physical key.

Property Type Description
RepeatCount uint Gets the repeat count of the key press. A value of 1 indicates the first press. Higher values indicate a key repeat.
IsKeyOnline bool Gets a value that indicates whether the key is present. This is typically true for most standard keyboards.
IsCapsLockOn bool Gets a value that indicates whether the Caps Lock key is currently active.
IsNumLockOn bool Gets a value that indicates whether the Num Lock key is currently active.
IsScrollLockOn bool Gets a value that indicates whether the Scroll Lock key is currently active.
IsLeftShiftPressed bool Gets a value that indicates whether the left Shift key is currently pressed.
IsRightShiftPressed bool Gets a value that indicates whether the right Shift key is currently pressed.
IsLeftControlPressed bool Gets a value that indicates whether the left Control key is currently pressed.
IsRightControlPressed bool Gets a value that indicates whether the right Control key is currently pressed.
IsLeftAltPressed bool Gets a value that indicates whether the left Alt key is currently pressed.
IsRightAltPressed bool Gets a value that indicates whether the right Alt key is currently pressed.

Example Usage

Here's a basic example of how to handle keyboard events in a UWP application using C#.


// Assuming you have a UI element (e.g., a Page or a specific control)
// that can receive keyboard input.

// In your XAML, ensure the element has KeyUp or KeyDown events wired up,
// or set the `IsFocusEnabled` property if needed.

// Example in C# code-behind:
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        // Subscribe to the KeyDown event for the page
        this.KeyDown += MainPage_KeyDown;
        // Subscribe to the KeyUp event for the page
        this.KeyUp += MainPage_KeyUp;

        // Ensure the page can receive focus to handle keyboard events
        this.Focus(FocusState.Programmatic);
    }

    private void MainPage_KeyDown(object sender, KeyEventArgs e)
    {
        Debug.WriteLine($"Key Down: {e.VirtualKey}");

        // Example: Handle pressing the Enter key
        if (e.VirtualKey == VirtualKey.Enter)
        {
            Debug.WriteLine("Enter key pressed!");
            // Perform an action, e.g., submit a form
        }

        // Example: Check for modifier keys
        if (e.KeyStatus.IsControlPressed() && e.VirtualKey == VirtualKey.S)
        {
            Debug.WriteLine("Ctrl + S pressed (Save action)");
            // Trigger save operation
        }
    }

    private void MainPage_KeyUp(object sender, KeyEventArgs e)
    {
        Debug.WriteLine($"Key Up: {e.VirtualKey}");

        // Example: Handling a key release, perhaps to stop an action
        if (e.VirtualKey == VirtualKey.Space)
        {
            Debug.WriteLine("Spacebar released.");
        }
    }
}

// Helper extension methods for checking modifiers (can be defined elsewhere)
public static class KeyEventArgsExtensions
{
    public static bool IsModifierPressed(this CorePhysicalKeyStatus status)
    {
        return status.IsLeftShiftPressed || status.IsRightShiftPressed ||
               status.IsLeftControlPressed || status.IsRightControlPressed ||
               status.IsLeftAltPressed || status.IsRightAltPressed;
    }

    public static bool IsControlPressed(this CorePhysicalKeyStatus status)
    {
        return status.IsLeftControlPressed || status.IsRightControlPressed;
    }

    public static bool IsShiftPressed(this CorePhysicalKeyStatus status)
    {
        return status.IsLeftShiftPressed || status.IsRightShiftPressed;
    }

    public static bool IsAltPressed(this CorePhysicalKeyStatus status)
    {
        return status.IsLeftAltPressed || status.IsRightAltPressed;
    }
}
            
Note: For text input scenarios where you need to capture characters typed (including those with modifier keys like shift), consider using the CharacterReceived event provided by CoreDispatcher, which is often more appropriate for handling text composition.

Related Topics