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

  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds