Click here to Skip to main content
15,880,967 members
Articles / Desktop Programming / Windows Forms

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

Rate me:
Please Sign up or sign in to vote.
4.81/5 (9 votes)
20 Mar 2007CPOL4 min read 97.5K   7K   63   14
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:

C#
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.

C#
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:

C#
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)


Written By
Other
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to save the image? Pin
Masterhame24-Jul-19 23:56
Masterhame24-Jul-19 23:56 
QuestionIs it possible to capture whatever user has written/drawn into an image? Pin
Kutub198730-Oct-16 4:40
Kutub198730-Oct-16 4:40 
QuestionStylus Resolution Changing Pin
Member 1140064924-Mar-15 9:46
Member 1140064924-Mar-15 9:46 
QuestionQuestion Pin
parihar9021-Jan-15 21:02
parihar9021-Jan-15 21:02 
GeneralMy vote of 5 Pin
blulaa11-Apr-13 20:39
blulaa11-Apr-13 20:39 
QuestionWill you allow me to use / reference this in my Degree Dissertation? Pin
Member 429406220-Dec-07 3:01
Member 429406220-Dec-07 3:01 
GeneralWill this work for Windows XP on Touchscreen Kiosk Pin
dosdemon6-Aug-07 21:38
dosdemon6-Aug-07 21:38 
Generalpen tablet Pin
HOYAM10-Jul-07 19:02
HOYAM10-Jul-07 19:02 
GeneralPen or Mouse input Pin
VezPro28-Mar-07 8:06
VezPro28-Mar-07 8:06 
AnswerRe: Pen or Mouse input Pin
Todd Landstad3-Apr-07 7:45
Todd Landstad3-Apr-07 7:45 
GeneralRe: Pen or Mouse input Pin
Lukasz Sw.7-May-07 8:50
Lukasz Sw.7-May-07 8:50 
GeneralRe: Pen or Mouse input Pin
Todd Landstad11-Jun-07 10:33
Todd Landstad11-Jun-07 10:33 
You'll need to install the updated pen driver from Wacom (go to Drivers & Downloads on their site) in order for your external digitizer to be recognized properly. You should then see additional pressure data from the digitizer when collecting ink.
GeneralRe: Pen or Mouse input Pin
Lukasz Sw.21-Jun-07 0:27
Lukasz Sw.21-Jun-07 0:27 
GeneralRe: Pen or Mouse input Pin
ashishdhingra10-Nov-13 20:36
ashishdhingra10-Nov-13 20:36 

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

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