Click here to Skip to main content
Click here to Skip to main content

Handling Touch, Pen, or Mouse Digitizer input in your .NET Application

, 20 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple application that shows how you can determine whether user input is from a mouse, tablet digitizer, or touch.
Screenshot - TouchEnabledInputFigure.jpg

Introduction

Using the included Tablet Technology that is built into Windows Vista, you can enable your application to perform unique actions for different input devices such as pen, mouse and touch input - even on a desktop PC.

Background

With Windows Vista, the software features of the Tablet PC are built into the core OS. This means that you can add support for Ink, handwriting recognition and in your Vista application without having to think about re-distributing dll's or worrying about whether your user is on a Tablet PC or not. In Windows XP, you had to have a Tablet PC in order to even think about using a pen as input using the API's in the OS.

What can I do with this kind of feature?

Digitizers have been used for years by graphics artists and the Tablet PC hardware integrated that input right into the display. Now, touch (resistive and capacitive) digitizers and electro-magnetic digitizers can be found on Mobile PC's as well as desktop LCD displays. Some PC's even have dual-mode digitizers integrated into them so you can handwrite on the screen if the pen is in range and then will automatically switch to touch mode when you use your finger on the screen. I think this is a great opportunity to add new functionality to applications to make use of this.

Think about this scenario: You have a photo application that allows you to handwrite keywords right on the picture using a Pen (that's cool in itself) and then when you reach out and touch the display with your finger, the application switches to panning mode so you can move the photo around. Pretty cool.

Using the code

I used the existing RealTimeStylus sample application that is in the Windows SDK as the base for this sample. The RealTimeStylus object (RTS) allows you to collect data from an attached digitizer or mouse device and gives you the ability to manipulate that data on the fly. You can add, remove & re-order different plug-ins with RealTimeStylus, which means you can change the way the input is rendered dynamically as well.

Since the sample application has most of the functionality I need, I want to add the ability to determine the input device that the current user input is coming from. RealTimeStylus is part of the Tablet PC features, which implies the use of ink, but that doesn't have to be the case. I can just use the input from RTS for whatever I want.

Let's start with making sure we receive the correct events by subscribing to the DataInterestMask. I want to make sure we get notification of when the input starts, when the application is receiving data, and of any errors that might occur:

public DataInterestMask DataInterest
{
    get
    {
        return DataInterestMask.StylusDown | DataInterestMask.Packets | 
            DataInterestMask.Error;
    }
}

Next, I need to make sure I add handlers for the above notifications. The steps for determining where the input originated, and what to do with it are straightforward:

  1. In the StylusDown event, query for the originating input device type.
  2. In the Packets event, choose to do something with the inbound data. In my case, I'll draw different colored ellipses depending on which input device from which the packets are coming.

The StylusDown event is very similar to a MouseDown event - it notifies you that some input is about to happen. This gives us opportunity to query which input device that with which the user is interacting. The Tablet object here describes all the capabilities of the current input device, including the type, or DeviceKind enumeration.

public void StylusDown(RealTimeStylus sender, StylusDownData data) 
{
    Tablet currentTablet = 
        sender.GetTabletFromTabletContextId(data.Stylus.TabletContextId);

    if(currentTablet != null)
    {
        // Keep track of the current input device type
        tabletKind = currentTablet.DeviceKind;
    }        
}

Now that you know which input device is about to create data, a handler for the Packets event needs to be added. This simply switches on the current TabletDeviceKind that we kept track of in the previous handler and performs some different action for each device type. I'm just going to draw different colored ellipses for each input device:

public void Packets(RealTimeStylus sender,  PacketsData data)
{           
    // For each new packet received, extract the x,y data
    // and draw a small circle around the result.
    for (int i = 0; i < data.Count; i += data.PacketPropertyCount)
    {
        // Packet data always has x followed by y followed by the rest
        Point point = new Point(data[i], data[i+1]);

        // Since the packet data is in Ink Space coordinates, convert to 
        // Pixels
        point.X = (int)Math.Round((float)point.X * 
            (float)myGraphics.DpiX/2540.0F);
        point.Y = (int)Math.Round((float)point.Y * 
            (float)myGraphics.DpiY/2540.0F);

        // Draw a circle corresponding to the packet
        switch (this.tabletKind)
        {
            case TabletDeviceKind.Pen:
                // Make the packets from the stylus smaller and green
                myGraphics.DrawEllipse(Pens.Green, point.X - 2, point.Y - 2, 
                    4, 4);
                break;
            case TabletDeviceKind.Mouse:
                // Make the packets from the mouse/pointing device mid-sized 
                // and red
                myGraphics.DrawEllipse(Pens.Red, point.X - 2, point.Y - 2, 
                    10, 10);
                break;
            case TabletDeviceKind.Touch:
                // Make the packets from a finger/touch digitizer larger and 
                // blue
                myGraphics.DrawEllipse(Pens.Blue, point.X - 2, point.Y - 2, 
                   20, 20);
                break;
        }
    }
}

Points of Interest

The sample above is inspecting only the X and Y coordinates of the packets coming in from the Mouse, Touch or Pen devices. However, digitizers can have additional packet data types as well. Electro-magnetic digitizers (either built into Tablet PC's or USB external digitizers) can also have pressure data available. Adding this support would allow this sample application to alter the size of each ellipse drawn based on how hard the user was pressing on the digitizer. Perhaps that will be an update to this article.

These API's are new to Windows Vista and are available in .NET 2.0, C++ COM, and in .NET 3.0's Windows Presentation Foundation (WPF).

History

6th of March, 2007 - Created article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Todd Landstad
Other
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberblulaa11-Apr-13 21:39 
QuestionWill you allow me to use / reference this in my Degree Dissertation? PinmemberMember 429406220-Dec-07 4:01 
GeneralWill this work for Windows XP on Touchscreen Kiosk Pinmemberdosdemon6-Aug-07 22:38 
Generalpen tablet PinmemberHOYAM10-Jul-07 20:02 
GeneralPen or Mouse input PinmemberVezPro28-Mar-07 9:06 
AnswerRe: Pen or Mouse input PinmemberTodd Landstad3-Apr-07 8:45 
GeneralRe: Pen or Mouse input PinmemberLukasz Swiatkowski7-May-07 9:50 
GeneralRe: Pen or Mouse input PinmemberTodd Landstad11-Jun-07 11:33 
GeneralRe: Pen or Mouse input PinmemberLukasz Swiatkowski21-Jun-07 1:27 
GeneralRe: Pen or Mouse input Pinmemberashishdhingra10-Nov-13 21:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Mar 2007
Article Copyright 2007 by Todd Landstad
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid