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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Today's competitive marketplace requires the organization to frequently release and deploy applications at the pace of user demands, with reduced cost, risk, and increased quality. This book defines the basics of application release and deployment, and provides best practices for implementation with resources for a deeper dive. Inside you will find: The business and technical drivers behind automated application release and deployment. Evaluation guides for application release and deployment solutions. …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds