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

  • 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: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds