Windows UWP Input API Reference

This section provides comprehensive documentation for the Universal Windows Platform (UWP) input APIs, enabling developers to handle user interactions seamlessly across various devices.

Core Input Concepts

Understanding the fundamental principles of input handling in UWP is crucial for building responsive and accessible applications.

Pointer Input

Pointer input encompasses touch, mouse, pen, and other pointing device interactions. UWP provides a unified model for handling these events.

  • PointerEventArgs: Contains information about the pointer event, including pointer type, position, and pressure.
  • PointerDevice: Represents the physical input device.
  • PointerPoint: Represents a single point in a pointer contact.

Key Classes:

Windows.UI.Core.CoreDispatcher Windows.UI.Xaml.Input.PointerRoutedEventArgs Windows.Devices.Input.PointerDevice
// Example: Handling PointerPressed event in XAML
private void MyElement_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    var pointerPoint = e.GetCurrentPoint(MyElement);
    var position = pointerPoint.Position;
    System.Diagnostics.Debug.WriteLine($"Pointer pressed at: {position.X}, {position.Y}");
}
                    

Keyboard Input

Capture and process keyboard events for text input, navigation, and custom shortcuts.

  • KeyEventArgs: Provides information about a keyboard key press, including the virtual key code and whether modifier keys are pressed.
  • CoreVirtualKeyCodes: Enumeration of standard virtual key codes.

Key Classes:

Windows.UI.Core.KeyEventArgs Windows.System.VirtualKey
// Example: Handling KeyDown event
private void MyTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Enter)
    {
        System.Diagnostics.Debug.WriteLine("Enter key pressed!");
        e.Handled = true; // Mark event as handled
    }
}
                    

Gesture Recognition

Utilize built-in gesture recognition to simplify common user interactions like taps, swipes, and pinch-to-zoom.

  • ManipulationStartingRoutedEventArgs: Fired when a manipulation gesture begins.
  • ManipulationDeltaRoutedEventArgs: Fired during a manipulation gesture, providing translation, rotation, and scale deltas.
  • TappedRoutedEventArgs: Fired when a tap gesture is recognized.

Key Classes:

Windows.UI.Xaml.Input.GestureRecognizer Windows.UI.Xaml.Input.ManipulationModes
// Example: Enabling manipulation on an element
public MyControl()
{
    InitializeComponent();
    ManipulationMode = ManipulationModes.All; // Enable all manipulation gestures
}

private void MyElement_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    // Apply transformations based on e.Delta.Translation, e.Delta.Rotation, e.Delta.Scale
    System.Diagnostics.Debug.WriteLine($"Manipulation Delta: Translation={e.Delta.Translation.X}");
}
                    

Advanced Input Scenarios

Explore specialized APIs for handling complex input scenarios and device-specific features.

Pen and Ink

Leverage the power of pen input for rich drawing and annotation experiences.

  • PenDevice: Represents a pen input device.
  • InkPresenter: Provides APIs for rendering and managing ink strokes.
  • PenAndInkSettings: Configures pen-related behaviors.

Key Namespaces:

Windows.UI.Input.Pen Windows.UI.Xaml.Controls.InkToolbar

Controller and Gamepad Input

Support for game controllers, joysticks, and other input peripherals.

  • GameController: Base class for various game controllers.
  • RawGameController: Provides low-level access to controller inputs.
  • Gamepad: Represents a standard gamepad.

Key Namespaces:

Windows.Gaming.Input

Speech Input

Integrate voice commands and speech recognition into your UWP applications.

  • SpeechRecognizer: Allows applications to recognize speech.
  • SpeechRecognitionEngine: Manages the speech recognition process.
  • ContinuousRecognitionSession: Manages continuous speech recognition.

Key Namespaces:

Windows.Media.SpeechRecognition

Best Practices