Understanding Gesture Support in Windows 8 and 8.1

Overview

Microsoft designed Windows 8 with touch in mind from the ground up. This is evident from the fact that they offer 2 SKUs – Windows RT (which is the basic SKU, which is touch based) and Windows RT Pro (which is an advanced SKU, supporting other input methods).

Since touch is one of the main ways of providing input, it is very helpful for Windows Store developers to understand the various touch gestures supported by Windows 8 (including Windows 8.1). In our two part article on touch gestures, we will learn about all the touch gestures and how we can build applications that can support all the gestures.

Let's start by learning about some of the touch gestures supported in Windows 8 that we will cover in this article.

1. Tap – Tap is invoked by tapping once on an item. The tap gesture is generally used to open/select an item being tapped.

2. Slide – Sliding is invoked by dragging a finger on the screen. It is generally used for scrolling.

3. Swipe – Swiping is similar to sliding, with one difference. In a swipe gesture, you drag a finger in the opposite direction compared to a slide gesture. Swiping is generally used to invoke system/app commands.

Application developers can use the GestureRecognizer class to identify the type of user input and then act on it accordingly.

The GestureRecognizer class resides in the Windows.UI.Input namespace and recognizes gestures such as tap, double tap, sliding and swiping, amongst others.

To see how GestureRecognizer works, let's build a simple application that will handle user interactions such as tap, slide and swipe.

Hands-On

Create a new Windows Store application titled Windows8GestureDemo01.

New Project
New Project

We will add a textbox to display the type of gesture we register. We’ll name this textbox as textBoxGestureNotes.

Next, we include the namespace for GestureRecognizer in the code behind for Mainpage.xaml.

using Windows.UI.Input;

Next, we create a member variable of type GestureRecognizer.

public sealed partial class MainPage : Page
    {
        GestureRecognizer gestureRecognizer = new Windows.UI.Input.GestureRecognizer();
        
        public MainPage()

On page load, we will register callbacks for gesture events for tapping, right-tapping, dragging and cross-sliding.

private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            gestureRecognizer.Tapped += gestureRecognizer_Tapped;
            gestureRecognizer.RightTapped += gestureRecognizer_RightTapped;
            gestureRecognizer.CrossSliding += gestureRecognizer_CrossSliding;
            gestureRecognizer.Dragging += gestureRecognizer_Dragging;
        }

To ensure that we clean up, we will unregister these event handlers on the page unload event.

private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            gestureRecognizer.Tapped -= gestureRecognizer_Tapped;
            gestureRecognizer.RightTapped -= gestureRecognizer_RightTapped;
            gestureRecognizer.CrossSliding -= gestureRecognizer_CrossSliding;
            gestureRecognizer.Dragging -= gestureRecognizer_Dragging;
        }
 

In the constructor of the page, we will set the GestureSettings of the GestureRecognizer object to listen to taps, right-taps, double-taps and drags.

        public MainPage()
        {
            this.InitializeComponent();
            this.gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.Tap | Windows.UI.Input.GestureSettings.DoubleTap | Windows.UI.Input.GestureSettings.RightTap | Windows.UI.Input.GestureSettings.Drag;
            
        }

Finally, we will implement the event handlers for the gesture events. In these events, we will fill the textbox with the text about the relevant gesture we received.

void gestureRecognizer_Dragging(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.DraggingEventArgs args)
        {
            textBoxGestureNotes.Text = "Slide/swipe gesture recognized";
            
        }
 
        void gestureRecognizer_RightTapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.RightTappedEventArgs args)
        {
            textBoxGestureNotes.Text = "Right Tap gesture recognized";
            
        }
 
        void gestureRecognizer_CrossSliding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.CrossSlidingEventArgs args)
        {
            textBoxGestureNotes.Text = "Slide/swipe gesture on a single pivot recognized";
            
        }
 
        void gestureRecognizer_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args)
        {
            textBoxGestureNotes.Text = "Tap gesture recognized";
            
        }

Our application is now ready. You can deploy this on Windows 8 install and debug this to see the gesture recognition in action. If you have trouble following along, you can download the sample code below.

Summary

In this article, we learned how to programmatically handle gestures in Windows Store applications. In a subsequent article, we will see support for handling additional Windows 8 gestures.

About the Author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com



Related Articles

Downloads

Comments

  • example doesn't work for me

    Posted by Thorsten on 11/16/2014 09:18am

    The example doesn't work on my PC. You create and initialize the variable "gestureRecognizer" but how is this variable linked to the MainPage-class?

    Reply
  • Windows Phone

    Posted by Fra on 05/22/2014 06:49am

    Does this work in Windows phone 8.1 Runtime? Is there any working sample?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

Most Popular Programming Stories

More for Developers

RSS Feeds