Gesture​Recognizer Gesture​Recognizer Gesture​Recognizer Class

Definition

Provides gesture and manipulation recognition, event listeners, and settings.

public sealed class GestureRecognizer : IGestureRecognizerpublic sealed class GestureRecognizer : IGestureRecognizerPublic NotInheritable Class GestureRecognizer Implements IGestureRecognizer
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

You can create a gesture object for each appropriate element when your app starts. However, this approach might not scale well depending on the number of gesture objects you need to create (for example, a jigsaw puzzle with hundreds of pieces).

In this case, you can create gesture objects dynamically on a pointerdown event and destroy them on an MSGestureEnd event. This approach scales well, but does incur some overhead due to creating and releasing these objects.

Alternatively, you can statically allocate and dynamically manage a pool of reusable gesture objects.

Note

: This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

For more detail on how to use cross-slide functionality, see Guidelines for cross-slide. The threshold distances used by the cross-slide interaction are shown in the following diagram.

Screen shot showing the select and drag and drop processes.

The PivotRadius and PivotCenter properties are used only when single pointer input is detected. They have no effect on multiple pointer input. The value for these properties should be updated regularly during the interaction.

Rotation is supported by a GestureRecognizer only when manipulationRotate is set through the GestureSettings property.

Rotation is not supported for single pointer input if the value of PivotRadius is set to 0.

Examples

Here we set up a GestureRecognizer object with a collection of input event handlers for processing various pointer and gestures. For more information on how to listen to and handle Windows Runtime events, see .

Note

Use the target or currentTarget property of the event object instead of the GestureRecognizer object in the event handler.


// inputManager handles gesture recognition for this sample.
function inputManager(target) {
    // Initialize the gesture recognizer.
    var gr = new Windows.UI.Input.GestureRecognizer();

    // Turn off visual feedback for gestures.
    // Visual feedback for pointer input is still displayed. 
    gr.showGestureFeedback = false;

    // Configure gesture recognizer to process the following:
    // double tap               - start questions and timer.
    // tap                      - move to next question.
    // right tap                - show answer.
    // hold and hold with mouse - start clues.
    gr.gestureSettings =
        Windows.UI.Input.GestureSettings.tap |
        Windows.UI.Input.GestureSettings.doubleTap |
        Windows.UI.Input.GestureSettings.rightTap |
        Windows.UI.Input.GestureSettings.hold |
        Windows.UI.Input.GestureSettings.holdWithMouse;

    // Register event listeners for these gestures.
    gr.addEventListener('tapped', tappedHandler);
    gr.addEventListener("holding", holdingHandler);
    gr.addEventListener("righttapped", rightTappedHandler);

    // Register event listeners for DOM pointer events.
    // The event pointer(s) are passed to the gesture recognizer
    // for further processing.
    target.addEventListener('MSPointerDown', pointerDown, false);
    target.addEventListener('MSPointerMove', pointerMove, false);
    target.addEventListener('MSPointerUp', pointerUp, false);
    target.addEventListener("MSPointerOver", pointerOver, true);
    target.addEventListener("MSPointerOut", pointerOut, true);
}

// Handle the pointer move event.
// The holding gesture is routed through this event.
// If pointer move is not handled, holding will not fire.
function pointerMove(evt) {
   _eventLog.innerText += "pointer move || ";

   // Get intermediate PointerPoints
   var pps = evt.intermediatePoints;

   // Pass the array of PointerPoints to the gesture recognizer.
   gr.processMoveEvents(pps);
};

// The pointer move event must also be handled because the 
// holding gesture is routed through this event.
// If pointer move is not handled, holding will not fire.
// A holding event is fired approximately one second after 
// a pointer down if no subsequent movement is detected.
function holdingHandler(evt) {
    if (evt.holdingState == Windows.UI.Input.HoldingState.started) {
        _eventLog.innerText += "holding || ";
    } else if (evt.holdingState == Windows.UI.Input.HoldingState.completed) {
        _eventLog.innerText += "holding completed || ";
    } else {
        _eventLog.innerText += "holding canceled || ";
    }
}

namespace GesturesApp.Manipulations
{
    /// <summary>
    /// Thin wrapper around the <see cref="Windows.UI.Input.GestureRecognizer"/>, routes pointer events received by
    /// the manipulation target to the gesture recognizer.
    /// </summary>
    /// <remarks>
    /// Transformations during manipulations cannot be expressed in the coordinate space of the manipulation target.
    /// Instead they need be expressed with respect to a reference coordinate space, usually an ancestor (in the UI tree)
    /// of the element being manipulated.
    /// </remarks>
    public abstract class InputProcessor
    {
        protected Windows.UI.Input.GestureRecognizer _gestureRecognizer;

        // Element being manipulated
        protected Windows.UI.Xaml.FrameworkElement _target;
        public Windows.UI.Xaml.FrameworkElement Target
        {
            get { return _target; }
        }

        // Reference element that contains the coordinate space used for expressing transformations 
        // during manipulation, usually the parent element of Target in the UI tree
        protected Windows.UI.Xaml.Controls.Canvas _reference;
        public Windows.UI.Xaml.FrameworkElement Reference
        {
            get { return _reference; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="element">
        /// Manipulation target.
        /// </param>
        /// <param name="reference">
        /// Element that contains the coordinate space used for expressing transformations
        /// during manipulations, usually the parent element of Target in the UI tree.
        /// </param>
        /// <remarks>
        /// Transformations during manipulations cannot be expressed in the coordinate space of the manipulation target.
        /// Thus <paramref name="element"/> and <paramref name="reference"/> must be different. Usually <paramref name="reference"/>
        /// will be an ancestor of <paramref name="element"/> in the UI tree.
        /// </remarks>
        internal InputProcessor(Windows.UI.Xaml.FrameworkElement element, Windows.UI.Xaml.Controls.Canvas reference)
        {
            this._target = element;
            this._reference = reference;

            // Setup pointer event handlers for the element.
            // They are used to feed the gesture recognizer.    
            this._target.PointerCanceled += OnPointerCanceled;
            this._target.PointerMoved += OnPointerMoved;
            this._target.PointerPressed += OnPointerPressed;
            this._target.PointerReleased += OnPointerReleased;
            this._target.PointerWheelChanged += OnPointerWheelChanged;

            // Create the gesture recognizer
            this._gestureRecognizer = new Windows.UI.Input.GestureRecognizer();
            this._gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.None;
        }

        private void OnPointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
        {
            // Route the events to the gesture recognizer.
            // All intermediate points are passed to the gesture recognizer in
            // the coordinate system of the reference element.
            this._gestureRecognizer.ProcessMoveEvents(args.GetIntermediatePoints(this._reference));

            // Mark event handled, to prevent execution of default event handlers
            args.Handled = true;
        }
    }
}

Remove the event handler and set the gesture recognizer to null if it is no longer needed:


gr.removeEventListener('MSPointerDown', pointerDown);
gr = null;
this._gestureRecognizer = null;

Constructors

GestureRecognizer() GestureRecognizer() GestureRecognizer()

Initializes a new instance of a GestureRecognizer object.

public GestureRecognizer()public GestureRecognizer()Public Sub New()
Attributes

Remarks

The following is a list of default GestureRecognizer property values.

Properties

AutoProcessInertia AutoProcessInertia AutoProcessInertia

Gets or sets a value that indicates whether manipulations during inertia are generated automatically.

public PlatForm::Boolean AutoProcessInertia { get; set; }public bool AutoProcessInertia { get; set; }Public ReadWrite Property AutoProcessInertia As bool
Value
bool bool bool

True if manipulations are generated automatically; otherwise false. The default value is true.

Attributes

Remarks

If the value for AutoProcessInertia is set to false, applications must call ProcessInertia to handle events during inertia.

CrossSlideExact CrossSlideExact CrossSlideExact

Gets or sets a value that indicates whether the exact distance from initial contact to end of the cross-slide interaction is reported.By default, a small distance threshold is subtracted from the first position reported by the system for cross-slide interactions. If this flag is set, the distance threshold is not subtracted from the initial position.

Note

This distance threshold is intended to account for any slight movement of the contact after initial detection. It helps the system differentiate between cross-sliding and panning, and helps ensure that a tap gesture is not interpreted as either.

public PlatForm::Boolean CrossSlideExact { get; set; }public bool CrossSlideExact { get; set; }Public ReadWrite Property CrossSlideExact As bool
Value
bool bool bool

True if the distance threshold is not subtracted; otherwise false. The default value is false.

Attributes

CrossSlideHorizontally CrossSlideHorizontally CrossSlideHorizontally

Gets or sets a value that indicates whether the cross-slide axis is horizontal.

public PlatForm::Boolean CrossSlideHorizontally { get; set; }public bool CrossSlideHorizontally { get; set; }Public ReadWrite Property CrossSlideHorizontally As bool
Value
bool bool bool

True if the cross-slide axis is horizontal; otherwise false. The default value is false.

Attributes

CrossSlideThresholds CrossSlideThresholds CrossSlideThresholds

Gets or sets values that indicate the distance thresholds for a CrossSliding interaction.

public CrossSlideThresholds CrossSlideThresholds { get; set; }public CrossSlideThresholds CrossSlideThresholds { get; set; }Public ReadWrite Property CrossSlideThresholds As CrossSlideThresholds
Value
CrossSlideThresholds CrossSlideThresholds CrossSlideThresholds

By default, every value in CrossSlideThresholds is set to 0.0 (CrossSliding functionality is disabled).

Attributes

GestureSettings GestureSettings GestureSettings

Gets or sets a value that indicates the gesture and manipulation settings supported by an application.

public GestureSettings GestureSettings { get; set; }public GestureSettings GestureSettings { get; set; }Public ReadWrite Property GestureSettings As GestureSettings
Value
GestureSettings GestureSettings GestureSettings

The gesture settings supported by an application. The value of this property is a bitwise OR of members of GestureSettings enumeration.

Attributes

InertiaExpansion InertiaExpansion InertiaExpansion

Gets or sets a value that indicates the relative change in size of an object from the start of inertia to the end of inertia (when resizing, or scaling, is complete).

public float InertiaExpansion { get; set; }public float InertiaExpansion { get; set; }Public ReadWrite Property InertiaExpansion As float
Value
float float float

The relative change in size, in device-independent pixel (DIP).

Attributes

InertiaExpansionDeceleration InertiaExpansionDeceleration InertiaExpansionDeceleration

Gets or sets a value that indicates the rate of deceleration from the start of inertia to the end of inertia (when the resizing, or expansion, manipulation is complete).

public float InertiaExpansionDeceleration { get; set; }public float InertiaExpansionDeceleration { get; set; }Public ReadWrite Property InertiaExpansionDeceleration As float
Value
float float float

The rate of deceleration, in device-independent pixel (DIP)/ms2.

Attributes

Remarks

If InertiaExpansion is assigned a value, InertiaExpansionDeceleration is ignored.

InertiaRotationAngle InertiaRotationAngle InertiaRotationAngle

Gets or sets a value that indicates the final angle of rotation of an object at the end of inertia (when the rotation manipulation is complete).

public float InertiaRotationAngle { get; set; }public float InertiaRotationAngle { get; set; }Public ReadWrite Property InertiaRotationAngle As float
Value
float float float

The relative change in angle of rotation, in degrees.

Attributes

InertiaRotationDeceleration InertiaRotationDeceleration InertiaRotationDeceleration

Gets or sets a value that indicates the rate of deceleration from the start of inertia to the end of inertia (when the rotation manipulation is complete).

public float InertiaRotationDeceleration { get; set; }public float InertiaRotationDeceleration { get; set; }Public ReadWrite Property InertiaRotationDeceleration As float
Value
float float float

The rate of deceleration, in degrees/ms2.

Attributes

Remarks

If InertiaRotationAngle is assigned a value, InertiaRotationDeceleration is ignored.

InertiaTranslationDeceleration InertiaTranslationDeceleration InertiaTranslationDeceleration

Gets or sets a value that indicates the rate of deceleration from the start of inertia to the end of inertia (when the translation manipulation is complete).

public float InertiaTranslationDeceleration { get; set; }public float InertiaTranslationDeceleration { get; set; }Public ReadWrite Property InertiaTranslationDeceleration As float
Value
float float float

The rate of deceleration, in device-independent pixel (DIP)/ms2.

Attributes

Remarks

If InertiaTranslationDisplacement is assigned a value, InertiaTranslationDeceleration is ignored.

InertiaTranslationDisplacement InertiaTranslationDisplacement InertiaTranslationDisplacement

Gets or sets a value that indicates the relative change in the screen location of an object from the start of inertia to the end of inertia (when the translation manipulation is complete).

public float InertiaTranslationDisplacement { get; set; }public float InertiaTranslationDisplacement { get; set; }Public ReadWrite Property InertiaTranslationDisplacement As float
Value
float float float

The relative change in screen location, in device-independent pixel (DIP).

Attributes

IsActive IsActive IsActive

Gets a value that indicates whether an interaction is being processed.

public PlatForm::Boolean IsActive { get; }public bool IsActive { get; }Public ReadOnly Property IsActive As bool
Value
bool bool bool

True if the interaction (including inertia) is still being processed; otherwise false. The default value is false.

Attributes

IsInertial IsInertial IsInertial

Gets a value that indicates whether a manipulation is still being processed during inertia (no input points are active).

public PlatForm::Boolean IsInertial { get; }public bool IsInertial { get; }Public ReadOnly Property IsInertial As bool
Value
bool bool bool

True if the manipulation is still being processed during inertia; otherwise false. The default value is false.

Attributes

ManipulationExact ManipulationExact ManipulationExact

Gets or sets a value that indicates whether the exact distance from initial contact to end of the interaction is reported.By default, a small distance threshold is subtracted from the first delta reported by the system. This distance threshold is intended to account for slight movements of the contact when processing a tap gesture. If this flag is set, the distance threshold is not subtracted from the first delta.

public PlatForm::Boolean ManipulationExact { get; set; }public bool ManipulationExact { get; set; }Public ReadWrite Property ManipulationExact As bool
Value
bool bool bool

True if the distance threshold is subtracted; otherwise false. The default value is false.

Attributes

MouseWheelParameters MouseWheelParameters MouseWheelParameters

Gets a set of properties that are associated with the wheel button of a mouse device.

public MouseWheelParameters MouseWheelParameters { get; }public MouseWheelParameters MouseWheelParameters { get; }Public ReadOnly Property MouseWheelParameters As MouseWheelParameters
Value
MouseWheelParameters MouseWheelParameters MouseWheelParameters

The collection of wheel button properties. The system defaults should be checked to ensure the best user experience for your app.

Attributes

Remarks

The properties exposed through MouseWheelParameters are used in conjunction with the Wheel settings in the Mouse control panel to define the UI experience when using the wheel button.

PivotCenter PivotCenter PivotCenter

Gets or sets the center point for a rotation interaction when single pointer input is detected.

public Point PivotCenter { get; set; }public Point PivotCenter { get; set; }Public ReadWrite Property PivotCenter As Point
Value
Point Point Point

The screen location for the center of rotation, in device-independent pixel (DIP).

Attributes

PivotRadius PivotRadius PivotRadius

Gets or sets the radius, from the PivotCenter to the pointer input, for a rotation interaction when single pointer input is detected.

public float PivotRadius { get; set; }public float PivotRadius { get; set; }Public ReadWrite Property PivotRadius As float
Value
float float float

The offset between the PivotCenter point and the single pointer input, in device-independent pixel (DIP).

Attributes

ShowGestureFeedback ShowGestureFeedback ShowGestureFeedback

Gets or sets a value that indicates whether visual feedback is displayed during an interaction.

public PlatForm::Boolean ShowGestureFeedback { get; set; }public bool ShowGestureFeedback { get; set; }Public ReadWrite Property ShowGestureFeedback As bool
Value
bool bool bool

True if feedback is displayed; otherwise false. The default is true.

Attributes

Methods

CanBeDoubleTap(PointerPoint) CanBeDoubleTap(PointerPoint) CanBeDoubleTap(PointerPoint)

Identifies whether a tap can still be interpreted as the second tap of a double tap gesture.

public PlatForm::Boolean CanBeDoubleTap(PointerPoint value)public bool CanBeDoubleTap(PointerPoint value)Public Function CanBeDoubleTap(value As PointerPoint) As bool
Parameters
value
PointerPoint PointerPoint PointerPoint

The last input pointer.

Returns
bool bool bool

True if a UI element supports the double tap gesture and the time threshold to complete the gesture has not been crossed; otherwise false.

Attributes

Remarks

This method supports GestureRecognizer object pool management, where GestureRecognizer objects are dynamically assigned to a set of UI elements.

Specifically, GestureRecognizer objects can be recycled for UI elements that do not support or can no longer process a double tap gesture. For example, if CanBeDoubleTap returns true for the most recent PointerPoint, it’s not safe to recycle the GestureRecognizer because a double tap is still a viable interaction based on subsequent input. If CanBeDoubleTap returns false, the GestureRecognizer can be safely recycled.

This method is not required if GestureRecognizer object pool management is not implemented.

CompleteGesture() CompleteGesture() CompleteGesture()

Causes the gesture recognizer to finalize an interaction.

public void CompleteGesture()public void CompleteGesture()Public Function CompleteGesture() As void
Attributes

Remarks

This method sets IsActive and IsInertial to false, based on the current state of the interaction. Depending on the interaction one of the following events is raised:

ProcessDownEvent(PointerPoint) ProcessDownEvent(PointerPoint) ProcessDownEvent(PointerPoint)

Processes pointer input and raises the GestureRecognizer events appropriate to a pointer down action for the gestures and manipulations specified by the GestureSettings property.

public void ProcessDownEvent(PointerPoint value)public void ProcessDownEvent(PointerPoint value)Public Function ProcessDownEvent(value As PointerPoint) As void
Parameters
value
PointerPoint PointerPoint PointerPoint

The input point.

Attributes

ProcessInertia() ProcessInertia() ProcessInertia()

Performs inertia calculations and raises the various inertia events.

public void ProcessInertia()public void ProcessInertia()Public Function ProcessInertia() As void
Attributes

ProcessMouseWheelEvent(PointerPoint, Boolean, Boolean) ProcessMouseWheelEvent(PointerPoint, Boolean, Boolean) ProcessMouseWheelEvent(PointerPoint, Boolean, Boolean)

Processes pointer input and raises the GestureRecognizer events appropriate to a mouse wheel action for the gestures and manipulations specified by the GestureSettings property.

public void ProcessMouseWheelEvent(PointerPoint value, Boolean isShiftKeyDown, Boolean isControlKeyDown)public void ProcessMouseWheelEvent(PointerPoint value, Boolean isShiftKeyDown, Boolean isControlKeyDown)Public Function ProcessMouseWheelEvent(value As PointerPoint, isShiftKeyDown As Boolean, isControlKeyDown As Boolean) As void
Parameters
value
PointerPoint PointerPoint PointerPoint

The input point.

isShiftKeyDown
System.Boolean System.Boolean System.Boolean

True if the Shift key is pressed; otherwise false.

isControlKeyDown
System.Boolean System.Boolean System.Boolean

True if the Ctrl key is pressed.

Attributes

ProcessMoveEvents(IVector<PointerPoint>) ProcessMoveEvents(IVector<PointerPoint>) ProcessMoveEvents(IVector<PointerPoint>)

Processes pointer input and raises the GestureRecognizer events appropriate to a pointer move action for the gestures and manipulations specified by the GestureSettings property.

public void ProcessMoveEvents(IVector<PointerPoint> value)public void ProcessMoveEvents(IVector<PointerPoint> value)Public Function ProcessMoveEvents(value As IVector<PointerPoint>) As void
Parameters
value

The pointer location history based on the PointerId . If no history is available then the value is the current location of the input pointer.

Attributes

ProcessUpEvent(PointerPoint) ProcessUpEvent(PointerPoint) ProcessUpEvent(PointerPoint)

Processes pointer input and raises the GestureRecognizer events appropriate to a pointer up action for the gestures and manipulations specified by the GestureSettings property.

public void ProcessUpEvent(PointerPoint value)public void ProcessUpEvent(PointerPoint value)Public Function ProcessUpEvent(value As PointerPoint) As void
Parameters
value
PointerPoint PointerPoint PointerPoint

The input point.

Attributes

Events

CrossSliding CrossSliding CrossSliding

Occurs when a user performs a slide or swipe gesture (through a single touch contact) within a content area that supports panning along a single axis only. The gesture must occur in a direction that is perpendicular to this panning axis.

Note

A swipe is a short sliding gesture that results in a selection action while the longer slide gesture crosses a distance threshold and results in a rearrange action. The swipe and slide gestures are demonstrated in the following diagram. Diagram showing the select and drag actions.

public event TypedEventHandler CrossSlidingpublic event TypedEventHandler CrossSlidingPublic Event CrossSliding
Attributes

Remarks

CrossSlide must be set in the GestureSettings property to support CrossSliding.

CrossSliding distance thresholds are disabled by default. Use CrossSlideThresholds to set these values.

Dragging Dragging Dragging

Occurs when a user performs a slide or swipe gesture with a mouse or pen/stylus (single contact).

public event TypedEventHandler Draggingpublic event TypedEventHandler DraggingPublic Event Dragging
Attributes

Remarks

Drag must be set in the GestureSettings property to support Dragging.

Holding Holding Holding

Occurs when a user performs a press and hold gesture (with a single touch, mouse, or pen/stylus contact).

public event TypedEventHandler Holdingpublic event TypedEventHandler HoldingPublic Event Holding
Attributes

Remarks

To support Holding with touch and pen/stylus input, specify Hold in the GestureSettings property.

To support Holding with mouse input, specify HoldWithMouse in the GestureSettings property.

The Holding event is fired twice: Once when holding starts (started ) and again when holding ends (completed or canceled ).

ManipulationCompleted ManipulationCompleted ManipulationCompleted

Occurs when the input points are lifted and all subsequent motion (translation, expansion, or rotation) through inertia has ended.

public event TypedEventHandler ManipulationCompletedpublic event TypedEventHandler ManipulationCompletedPublic Event ManipulationCompleted
Attributes

ManipulationInertiaStarting ManipulationInertiaStarting ManipulationInertiaStarting

Occurs when all contact points are lifted during a manipulation and the velocity of the manipulation is significant enough to initiate inertia behavior (translation, expansion, or rotation continue after the input pointers are lifted).

public event TypedEventHandler ManipulationInertiaStartingpublic event TypedEventHandler ManipulationInertiaStartingPublic Event ManipulationInertiaStarting
Attributes

Remarks

Inertia is the tendency of an object to resist any change in its state of rest or its velocity.

This event is raised before inertia processing.

Inertia behavior for this manipulation can be customized in the handler for this event. For example, inertia can be set to end after a specific distance or UI is displayed.

Note

These settings cannot be customized after the event.

ManipulationStarted ManipulationStarted ManipulationStarted

Occurs when one or more input points have been initiated and subsequent motion (translation, expansion, or rotation) has begun.

public event TypedEventHandler ManipulationStartedpublic event TypedEventHandler ManipulationStartedPublic Event ManipulationStarted
Attributes

ManipulationUpdated ManipulationUpdated ManipulationUpdated

Occurs after one or more input points have been initiated and subsequent motion (translation, expansion, or rotation) is under way.

public event TypedEventHandler ManipulationUpdatedpublic event TypedEventHandler ManipulationUpdatedPublic Event ManipulationUpdated
Attributes

RightTapped RightTapped RightTapped

Occurs when the pointer input is interpreted as a right-tap gesture, regardless of input device.

  • Right mouse button click
  • Pen barrel button click
  • Touch or pen press and hold
public event TypedEventHandler RightTappedpublic event TypedEventHandler RightTappedPublic Event RightTapped
Attributes

Remarks

Listen for this event when you want to display a context menu.

Tapped Tapped Tapped

Occurs when the pointer input is interpreted as a tap gesture.

public event TypedEventHandler Tappedpublic event TypedEventHandler TappedPublic Event Tapped
Attributes

See Also