How Do I ... ? |
Catch Keyboard Events |
|
You can easily capture any keyboard event or keypress to improve the interaction and accessibility of your control applications.
There is a multimedia walk-through of these techniques in the online Technical Resources.
There is a built-in Keyboard port driver within the Generic Control Computer that is part of every Stardraw Control project. There is no special equipment that you require to catch and process keyboard events.
It is also possible to capture "hard buttons" on UMPCs or other devices that do not have conventional keyboards.
Although no design-time configuration is necessary, by default, the Keyboard does not emit any events at run-time, until we specifically ask it to.
Two methods of the Keyboard device allow us to control whether events are emitted:
StartListening activate keyboard events,
StopListening deactivates keyboard events.
Before any form can capture keyboard events, the StartListening method must be called first to activate events. It is convenient to call this method in the Form's Load event.
Tip
You only need to call the StartListening method once in your application: if you have more than one form in your project, only the first form that opens (i.e. the default form) needs to call the StartListening method: all forms that are subsequently opened will be able to capture keyboard events.
In the Forms View, double-click
the form to display the Actions Editor.
Note that the selected event is Form1.Load:
this is the event that we wish to capture to activate keyboard events.
Add the following Action
to call the StartListening method:
Devices –
Computer (1) –
Keyboard –
StartListening
Click Ok to close the Actions dialog.
Your application is now ready to receive keyboard events.
The Keyboard device includes events for every single key on a standard keyboard; including function keys, cursor keys, modifiers such as Shift, Ctrl, Alt, etc. plus vendor-specific keys such as the Windows key (ÿ), hard buttons and media and application control keys often found on UMPC and Tablet PCs.
Because the list of available key events is exhaustive, it can be difficult to determine the precise key event to capture if it's not an obvious one, like a letter, number or function key.
One of the Keyboard events is KeyPressed, which is fired for all keypresses, regardless of actual key. The Keyboard device also has a LastKeyPressed property that stores the name of the key that caused the KeyPressed event to fire.
We can use this to more easily determine specific keys.
In the Forms View, from the Windows tab of the Toolbox click and drag a TextBox control onto the form.
In the properties for the TextBox control, clear the Text property of its default text.
Double-click the form
to display the Actions Editor, and select the event:
Devices –
Computer (1) –
Keyboard –
KeyPressed
Add the following Action
to display the LastKeyPressed
property:
Controls –
Form1 –
textBox1 –
Set Text to
Set
the Text property to:
Devices –
Computer (1) –
Keyboard –
LastKeyPressed
Click Ok to close the Actions dialog.
Run the project: when you press any key on the keyboard, the key's name will be displayed in the textbox.
Now that you can easily identify any key or hard button on your keyboard or mobile device, you can now capture and respond to specific keypresses.
In the Forms View, from the Controls tab of the Toolbox click and drag a Toggle control onto the form.
Double-click the form
to display the Actions Editor, and select the event:
Devices –
Computer (1) –
Keyboard –
PageUp
Add the following Action
to change the state of the Toggle control:
Controls –
Form1 –
toggle1 –
Set Checked to –
Variables –
True
Select the event:
Devices –
Computer (1) –
Keyboard –
PageUp
Add the following Action:
Controls –
Form1 –
toggle1 –
Set Checked to –
Variables –
False
Click Ok to close the Actions dialog.
Run the project: when you press the PageUp key, the toggle control will change its state to up, and will change to down when you press the PageDown key.