Share via


Windows.UI.Input.Preview.Injection Namespace

Provides support for programmatically generating and automating input from a variety of devices such as keyboard, mouse, touch, pen, and gamepad.

Important

The APIs in this namespace require the inputInjectionBrokered restricted capability.

Classes

InjectedInputGamepadInfo

Represents programmatically generated gamepad input.

InjectedInputKeyboardInfo

Represents programmatically generated keyboard input, such as a Tab or Shift+Tab (Reverse Tabbing).

InjectedInputMouseInfo

Represents programmatically generated mouse input.

InjectedInputPenInfo

Represents programmatically generated pen input.

InjectedInputTouchInfo

Represents programmatically generated touch input.

InputInjector

Represents the virtual input device for sending the input data.

Structs

InjectedInputPoint

Contains the screen coordinates of the pointer in device-independent pixel (DIP).

InjectedInputPointerInfo

Contains basic pointer information common to all pointer types.

InjectedInputRectangle

The offsets, from the injected pointer, for the bounding box that represents the touch contact area.

Enums

InjectedInputButtonChangeKind

Specifies the changes in state of a button associated with a pointer.

InjectedInputKeyOptions

Specifies the various options, or modifiers, used to simulate input from physical or virtual keyboards through InjectedInputKeyboardInfo.

InjectedInputMouseOptions

Specifies the various options, or modifiers, used to simulate mouse input through InjectedInputMouseInfo.

InjectedInputPenButtons

Specifies the pen options used to simulate pen input through InjectedInputPenInfo.

InjectedInputPenParameters

Specifies the pen states used to simulate pen input through InjectedInputPenInfo.

InjectedInputPointerOptions

Specifies the various options, or modifiers, used to simulate pointer input through InjectedInputMouseInfo, InjectedInputPenInfo, and InjectedInputTouchInfo.

InjectedInputShortcut

Specifies the system shortcuts for InjectShortcut.

InjectedInputTouchParameters

Specifies the touch states used to simulate touch input through InjectedInputTouchInfo.

InjectedInputVisualizationMode

Specifies the type of visual feedback displayed for the injected input type.

Examples

Here's an example of a touch input injection function.

First, we call TryCreate to instantiate the InputInjector object.

Then, we call InitializeTouchInjection with an InjectedInputVisualizationMode of Default.

After calculating the point of injection, we call InjectedInputTouchInfo to initialize the list of touch points to inject (for this example, we create one touch point corresponding to the mouse input pointer).

Finally, we call InjectTouchInput twice, the first for a pointer down and the second for a pointer up.

/// <summary>
/// Inject touch input on injection target corresponding 
/// to mouse click on input target.
/// </summary>
/// <param name="pointerPoint">The mouse click pointer.</param>
private void InjectTouchForMouse(PointerPoint pointerPoint)
{
    // Create the touch injection object.
    _inputInjector = InputInjector.TryCreate();

    if (_inputInjector != null)
    {
        _inputInjector.InitializeTouchInjection(
            InjectedInputVisualizationMode.Default);

        // Create a unique pointer ID for the injected touch pointer.
        // Multiple input pointers would require more robust handling.
        uint pointerId = pointerPoint.PointerId + 1;

        // Get the bounding rectangle of the app window.
        Rect appBounds =
            Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;

        // Get the top left screen coordinates of the app window rect.
        Point appBoundsTopLeft = new Point(appBounds.Left, appBounds.Top);

        // Get a reference to the input injection area.
        GeneralTransform injectArea =
            ContainerInject.TransformToVisual(Window.Current.Content);

        // Get the top left screen coordinates of the input injection area.
        Point injectAreaTopLeft = injectArea.TransformPoint(new Point(0, 0));

        // Get the screen coordinates (relative to the input area) 
        // of the input pointer.
        int pointerPointX = (int)pointerPoint.Position.X;
        int pointerPointY = (int)pointerPoint.Position.Y;

        // Create the point for input injection and calculate its screen location.
        Point injectionPoint =
            new Point(
                appBoundsTopLeft.X + injectAreaTopLeft.X + pointerPointX,
                appBoundsTopLeft.Y + injectAreaTopLeft.Y + pointerPointY);

        // Create a touch data point for pointer down.
        // Each element in the touch data list represents a single touch contact. 
        // For this example, we're mirroring a single mouse pointer.
        List<InjectedInputTouchInfo> touchData =
            new List<InjectedInputTouchInfo>
            {
                new InjectedInputTouchInfo
                {
                    Contact = new InjectedInputRectangle
                    {
                        Left = 30, Top = 30, Bottom = 30, Right = 30
                    },
                    PointerInfo = new InjectedInputPointerInfo
                    {
                        PointerId = pointerId,
                        PointerOptions =
                        InjectedInputPointerOptions.PointerDown |
                        InjectedInputPointerOptions.InContact |
                        InjectedInputPointerOptions.New,
                        TimeOffsetInMilliseconds = 0,
                        PixelLocation = new InjectedInputPoint
                        {
                            PositionX = (int)injectionPoint.X ,
                            PositionY = (int)injectionPoint.Y
                        }
                },
                Pressure = 1.0,
                TouchParameters =
                    InjectedInputTouchParameters.Pressure |
                    InjectedInputTouchParameters.Contact
            }
        };

        // Inject the touch input. 
        _inputInjector.InjectTouchInput(touchData);

        // Create a touch data point for pointer up.
        touchData = new List<InjectedInputTouchInfo>
        {
            new InjectedInputTouchInfo
            {
                PointerInfo = new InjectedInputPointerInfo
                {
                    PointerId = pointerId,
                    PointerOptions = InjectedInputPointerOptions.PointerUp
                }
            }
        };

        // Inject the touch input. 
        _inputInjector.InjectTouchInput(touchData);
    }
}

Here are some downloadable samples demonstrating basic input and input injection:

Remarks

Using input injection requires the following be added to the Package.appxmanifest:

  • To <Package>
    • xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    • IgnorableNamespaces="rescap"
  • To <Capabilities>
    • <rescap:Capability Name="inputInjectionBrokered" />

See also