Not sure how many of you have needed to use the Microsoft.Ink SDK, but
I am currently learning how to use the SDK on my current project. I
have to say, that the reconizition that they provide is really, really
good.
The Ink SDK comes with a TextBox and a PictureBox, but I only need the
Text Box, so that is what I will work with here. When using the
TextBox, it is almost as straight forward as the standard TextBox, but
you will need to set a few properties before you can achive the results
you are looking for.
In this post I will show you how to setup a InkText Box for recognition with Wordlist Validation. So lets get started.
Step 1 - Creating the Recognizer Context
// Create the local context recognization, this will be used during the recognition
private RecognizerContext _context = new RecognizerContext();
Step 2 - Wired up the needed events
// Needed for the recognition to actuall kick off
inkEdit.Recognition += new InkEditRecognitionEventHandler( inkEdit_Recognition );
// Needed so we can run recognition with alternates
Context.RecognitionWithAlternates += new RecognizerContextRecognitionWithAlternatesEventHandler( Context_RecognitionWithAlternates );
Step 3 - Setting some default properties on your InkEdit TextBox
// This needs to be set to define the mode the control is in.
// We do not want to listen for gestures, so we dont use InkAndGesture
this.inkEdit.InkMode = Microsoft.Ink.InkMode.Ink;
// When writing in the text box the controll will not perform immediate reconigition so this
// tells the controll how long to wait before it attemps to reconize your handwriting to text
this.inkEdit.RecoTimeout = 1000;
// If you dont set this to true, you will not be able to use the mouse to 'write'
this.inkEdit.UseMouseForInput = true;
Step 4 - Prepare the Context and word list.
// Create the recognization
Context = new RecognizerContext();
// The wordlist object here needs to be populated by you with the list of valid values
// that can be reconized. This can be a list of zip codes for example
Context.WordList = wordList;
// If using a word list, then the factoid needs to be set to WordList
Context.Factoid = Factoid.WordList;
Context.RecognitionFlags = RecognitionModes.WordMode | RecognitionModes.Coerce;
Step 5 - Code for the recognition
// copy the strokes from the InkEdit control into the new
// RecognizerContext
Context.Strokes = e.RecognitionResult.Strokes.Ink.Clone().Strokes;
// Kickoff the bacground recognization
Context.BackgroundRecognizeWithAlternates();
Step 6 - Listen ot the background recognition
private void Context_RecognitionWithAlternates( object sender, RecognizerContextRecognitionWithAlternatesEventArgs e )
{
// Because the background recogizition runs on a seperate thread, we need to check for
// invoke so we dont do cross thread calls.
if ( InvokeRequired )
{
BeginInvoke( new RecognizerContextRecognitionWithAlternatesEventHandler( Context_RecognitionWithAlternates ), sender, e );
return;
}
// I clear the values, because i only want valid data from the word list.
inkEdit.Clear();
// I only want to allow valid data in my Ink Text Box, so if the confidence is Poor, then i will clearn
// the values
if ( e.Result.TopConfidence != RecognitionConfidence.Poor )
{
// Since we have a solid confidence level, set the top match value
inkEdit.SelectedText = e.Result.TopString;
inkEdit.SelectionStart = inkEdit.SelectionStart + e.Result.TopString.Length;
}
else
{
// Clearing the text box because we only allow values that have a solid confidence level
inkEdit.SelectedText = string.empty;
// If you wanted to do something with the alternate values from the wordlist, do i there
HandleAlternates( e.Result.GetAlternatesFromSelection() );
}
Well there you have it, a clean cut example to using the Microsoft.Ink text box
with WordList Recognition.
Posted
10-24-2006 6:33 AM
by
Derik Whittaker