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

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • Finance leaders have been talking about expanding the value-added role of their teams for a long time. The debate is no longer whether the finance and accounting function needs to become a more strategic partner to the rest of the business but rather how to get there. Technology innovation has caught up to this ambition, and what was once aspiration can be a reality – and the choice is now yours. Read this research report to learn how to make the most of information tools to enable innovation and growth.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date