RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Get Control and Performance with the Real Time Stylus API

The Real Time Stylus API provides an alternate way to receive pen input pen. This high performance API provides a great level of control to developers for a small penalty in added effort.

tandard Ink collection is incredibly easy using the Tablet PC SDK; however, for scenarios that require more control or non-default behavior, default Ink collection may not be the best choice. The Real Time Stylus API provides lower-level, higher performance access to stylus input, making this the API of choice for power-developers.

Many Tablet PC applications can do everything they need to do using Tablet PC SDK controls and classes such as the InkCollector and InkOverlay. These objects fully automate the collection of Ink information received from the stylus (pen), rendering that Ink on the screen, and even collecting that Ink into memory using Ink objects, stroke collections, and data points. These objects are great for the most fundamental thing Tablet PCs can do: collecting digital Ink.

There are many scenarios where a developer might want to have more control over this process, or perhaps apply a different behavior and use the stylus for things other than collecting Ink, or maybe to collect and draw Ink in a non-standard fashion. There also are many scenarios where the performance of standard Ink collectors is not sufficient. Examples include games, 3D applications where the stylus is used to move objects around, and other real-time applications.

For all these needs, the Tablet PC SDK version 1.7 introduced a new way of getting to pen data: the Real Time Stylus. The Real Time Stylus API is very simple in concept. The digitizer collects data from the hardware and passes the information on to a collection of registered objects. These objects are known as stylus plug-in objects. There are two kinds of those plug-in objects: those that implement the IStylusSyncPlugin interface, and those that implement IStylusAsyncPlugin. These two object types are very similar in functionality. For the first examples, I will focus on IStylusSyncPlugin.

The Basic Implementation
To create a project implementing an object that reacts to real-time stylus events, create a standard VB.NET or C# project, and include the Microsoft.Ink (also known as Tablet PC SDK) assembly/namespace in your references.

The Real Time Stylus API is all about control and performance.
The next step is the creation of an object that can be registered with the digitizer so it can be called whenever the pen triggers some action you are interested in. This object needs to implement the IStylusSyncPlugin interface, which is very simple in concept. It has a number of methods that get called whenever a certain action happens. These methods can conceptually be seen as event handlers. Keep in mind that the Real Time Stylus API is all about performance. For this reason, not all the available methods are called all the time. Instead, each plug-in object must implement a property that indicates which events you are interested in.

For instance, if you are interested in all packets (data generated when the pen is used to write or draw) as well as pen up/down events, you can indicate this using the DataInterest property as shown in the following VB.Net example:

   Public ReadOnly Property DataInterest() _
      As DataInterestMask _
      Implements IStylusSyncPlugin.DataInterest
         Return DataInterestMask.Packets _
            Or DataInterestMask.StylusDown _
            Or DataInterestMask.StylusUp
      End Get
   End Property

Here is the C# version:

   public DataInterestMask DataInterest
           return DataInterestMask.Packets | 
               DataInterestMask.StylusDown | 

A plug-in object configured in this fashion has its Packets(), StylusDown(), and StylusUp() methods called whenever those events occur. They can be used in the following fashion:

   Public Sub Packets(ByVal s As RealTimeStylus, _
      ByVal data As PluginData.PacketsData) _
      Implements IStylusSyncPlugin.Packets
      Console.WriteLine("Packet: X " _
         + data(0).ToString() + _
          "/ Y " + data(1).ToString())
   End Sub
   Public Sub StylusDown(ByVal s As RealTimeStylus, _
      ByVal data As PluginData.StylusDownData) _
      Implements IStylusSyncPlugin.StylusDown
      Console.WriteLine("Stylus Down")
   End Sub
   Public Sub StylusUp(ByVal s As RealTimeStylus, _
      ByVal data As PluginData.StylusUpData) _
      Implements IStylusSyncPlugin.StylusUp
      Console.WriteLine("Stylus Up")
   End Sub

The C# version is very similar:

   public void Packets(RealTimeStylus sender, 
      PacketsData data)
      Console.WriteLine("Packet: X "
         + data[0].ToString() + 
         "/ Y " + data[1].ToString());
   public void StylusDown(RealTimeStylus sender, 
      StylusDownData data)
      Console.WriteLine("Stylus Down");
   public void StylusUp(RealTimeStylus sender, 
      StylusUpData data)
      Console.WriteLine("Stylus Up");

Listing 1 and Listing 2 show the complete implementation of this simple Real Time Stylus plug-in.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date