Touch Keyboard

When you are creating applications for kiosks and other devices which don't have a keyboard attached, it is nice to show one on screen and let users type by selecting these keys with their input device (possibly their finger, as using a virtual keyboard with the mouse is far from a nice experience).

The VCL in Delphi 2010 includes a framework for creating virtual keyboards, based on the new TouchKeyboard component and the two related units, Keyboard and KeyboardTypes. If you drop the TouchKeyboard component on a form at design time you'll see something like in the following image:

(^J Form!



isaaaaaiaaii iQiBBioaaiann iaiaiiassiiQi, :






â– sain

Of course, what you'll see depends on your active keyboard at the operating system level. What I've shown here is the output when I set the keyboard to "en-US". If I keep my standard settings, I'll generally see an Italian keyboard:

Keyboard Special Purpose Keys

Notice that while punctuation characters are indeed replaced by the corresponding elements in Italian, special purpose keys (like Esc, Caps, Del) are not modified. The issue is that the captions of these keys are not defined at the operating system, so the only option is to override the defaults at the TouchKeyboard component level, using its KeyCaptions property you can use to change individual captions, as shown here.

Another key property of the component is its ability to display a numeric keypad (like the one on the right) rather than a full keyboard, by changing the value of the Layout property (a string, not an enumeration, to allow future expansion and custom keyboard layouts) from Standard to Keypad71.

Regardless of the layout, the effect of using this virtual keyboard is to send input to the control currently having the input focus, like a physical keyboard. Of course, this means that the button representing keys won't get the focus when pressed, preserving the current input focus. You can see this effect in the basic KeyboardTest application (here when holding the Shift key):

E [KeyCaptions Esc

Bad&pace Tab Del Caps Enter LeftShift Rightshift LeftCtrl LeftAlt RightAlt RightCtrl

(Caption Overrides)

Delphi Keypad

71 You can create custom layouts, as Chris Bensen started explaining on his blog in the post and in the following 3 parts, showing also how to create a custom layout from an XML representation of the keyboard.

71 You can create custom layouts, as Chris Bensen started explaining on his blog in the post and in the following 3 parts, showing also how to create a custom layout from an XML representation of the keyboard.

The program also has a button that I've used to make a few experiments and try to find a workaround to an annoying VCL problem. As soon as I run a standard VCL application (including Delphi itself) it will reset my current keyboard to the default one. So whatever my keyboard setting a Delphi application will start with the Italian keyboard72.

In the button I've made two experiments, as you can see I the code below:

procedure TKeyboardForm.btnTestClick(Sender: TObject); var code: UINT; begin

LoadKeyboardLayout ('00000409', KLF_ACTIVATE);

code := MapVirtualKey(43, MAPVK_VSC_TO_VK); ShowMessage (IntToStr (code)); end;

First, I write the code need to force a different keyboard, specifically the 'en-US' keyboard, which has the keyboard layout code '00000409'. Loading and activating this layout broadcasts a wm_InputLanguageChange, which the TouchKeyboard component handles updating its layout. As an alternative you can call the ActivateKeyboardLayout function and Redraw the touch keyboard after letting the application process update messages:

ActivateKeyboardLayout(67699721 {en-US}, 0);



Second, I use the same code of the TouchKeyboard component for converting a virtual key into an actual character code, as an experiment. I used this code to figure out the VCL issue described earlier.

Overall the TouchKeyboard component can be handy in a kiosk or similar application, while in other occasions you might want to hide it and display it on request. Of course, you'll always need to have it on screen only while the user has to input some data, or you can prepare a standard "keyboard entry form" with a single edit box and a keyboard you show every time there is an input request (like when the actual edit in the main form received the focus). I've not created a demo program showing a similar situation, but it shouldn't be terribly difficult to create one.

72 The situation is actually worse, as running a Delphi program changes the active keyboard at the operating system setting, that is for each running application. Quite annoying, although few people use multiple settings.

Was this article helpful?

+3 0


  • rowan
    How to use ttouchkeyboard?
    7 years ago
  • maura cremonesi
    How to use a touch keyboard in delphi?
    1 year ago

Post a comment