Programmatically set Input Context features

In my last blog post I discussed how an end user could change word completion features at a system wide level. This blog post covers how a .NET Compact Framework developer can programatically change the behaviour on a textbox by textbox basis.

The Windows Mobile operating system provides a function called SHSetInputContext that enables a developer to configure the various properties of the input context associated with a given control.

By using a DllImport declaration we can gain access to this native function from our C# application as shown below:

using System.Runtime.InteropServices;
 
private enum SHIC_FEATURE : uint
{
  RESTOREDEFAULT = 0,
  AUTOCORRECT = 1,
  AUTOSUGGEST = 2,
  HAVETRAILER = 3,
  CLASS = 4
};
 
[DllImport("aygshell.dll")]
private static extern int SHSetInputContext(IntPtr hwnd,
  SHIC_FEATURE dwFeature,
  [MarshalAs(UnmanagedType.Bool)]ref bool lpValue);

Once declared it is as easy as passing a boolean true or false value to SHSetInputContext along with the name of the input context feature we want to configure.

For example to disable auto suggestion for a textbox called “textbox1″ we could use the following code snippet:

bool value = false;
SHSetInputContext(textbox1.Handle,
  SHIC_FEATURE.AUTOSUGGEST,
  ref value);

Sample application

[Download InputContextDemo.zip - 10.6KB]

A sample application is available for download. It wraps up the calls to SHSetInputContext into a handy InputContext class that can be re-used within your own applications.

The sample application has a single textbox and a series of checkboxes. The checkboxes enable you to control the various input context features present within the Word Completion tab of the Input control panel applet for the textbox.

We’ll round out our discussion of Input Contexts by covering Input Classes within the next blog post. In the mean time see if you can determine what changes in behaviour occur when you select the various options within the Input Class combo box.
Screenshot of sample application demonstrating setting Input Context features

Leave a Reply