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.

 

Online Resources

There is a multimedia walk-through of these techniques in the online Technical Resources.

 

You Will Need...

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.

 

Configuring the Keyboard Device

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:

 

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.

 

To create an Action that activates keyboard events

  1. 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.

  2. Add the following Action to call the StartListening method:
    Devices – Computer (1) – Keyboard – StartListening

  3. Click Ok to close the Actions dialog.

 

Your application is now ready to receive keyboard events.

 

Capturing 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.

 

To create an Action that shows all keypresses

  1. In the Forms View, from the Windows tab of the Toolbox click and drag a TextBox control onto the form.

  2. In the properties for the TextBox control, clear the Text property of its default text.

  3. Double-click the form to display the Actions Editor, and select the event:
    Devices – Computer (1) – Keyboard – KeyPressed

  4. Add the following Action to display the LastKeyPressed property:
    Controls – Form1 – textBox1 – Set Text to

  5. Set the Text property to:
    Devices – Computer (1) – Keyboard – LastKeyPressed

  6. 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.

 

To create an Action that responds to a specific keypress

  1. In the Forms View, from the Controls tab of the Toolbox click and drag a Toggle control onto the form.

  2. Double-click the form to display the Actions Editor, and select the event:
    Devices – Computer (1) – Keyboard – PageUp

  3. Add the following Action to change the state of the Toggle control:
    Controls – Form1 – toggle1 – Set Checked to – Variables – True

  4. Select the event:
    Devices – Computer (1) – Keyboard – PageUp

  5. Add the following Action:
    Controls – Form1 – toggle1 – Set Checked to – Variables – False

  6. 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.

 

See Also