Skip to content

Keyboard Events

The following functions handle various keyboard events.

Keyboard events are divided into “key typed” and “key pressed/released” events.

Key Typed Events

“Key typed” events are higher-level and generally are independent of platform (and keyboard layout). These are generated when a character is typed on the keyboard (typing means both pressing and releasing the character key(s) on the keyboard).

The following function is provided to handle “key typed” events. It is available for all GUI objects (e.g., Display, Circle, etc.):

Function Description
object.onKeyType(action) Set up a function to call when a key is typed (pressed and released).

Key Down/Up Events

“Key down” and “key up” events are lower level events and are generated whenever a key is pressed or released. As a result, these events may be used for various gaming applications (e.g., when pressing and holding a key does one thing, and when releasing the key does another).

These events are specific to the platform and keyboard layout (i.e., some keys may not work the same on all platforms). So, test on all desired platforms to make sure keystroke controls work as intended.

“Key down” and “key up” events are the only way to find out about keys that do not generate character input (e.g., action keys, modifier keys, etc.).

The following functions are provided to handle “key down” and “key up” events. They are available for all GUI objects (e.g., Display, Circle, etc.):

Function Description
object.onKeyDown(action) Set up a function to call when a key is pressed down.
object.onKeyUp(action) Set up a function to call when a key is released.

“Key down” and “key up” events use virtual key codes to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as “A”, which comes from shift and “a”).

For example, pressing the Shift key will cause a “key down” event with a VK_SHIFT key code; whereas pressing the ‘a’ key will result in a VK_A key code. After the ‘a’ key is released, a “key up” event will be fired with VK_A.

Here is a list of the most important virtual key codes:

  • VK_0 through VK_9 are for keys ‘0’ thru ‘9’.

  • VK_A through VK_Z are for keys ‘A’ thru ‘Z’ (regardless of case – upper/lower).

  • VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN are for the arrow keys.

  • VK_F1 through VK_F12 are for the function keys.

  • other keys, such as VK_AMPERSAND, VK_CAPS_LOCK, VK_COMMA, VK_CONTROL, VK_ENTER, VK_MINUS, VK_PLUS, VK_SPACE, and so on.