GML Functions: The Keyboard

Key-State Functions
To determine the state of a key you may use the following functions. These can be particularly useful for checking if multiple keys are pressed simultaneously.


 * keyboard_check(key) Returns whether the key with the particular keycode is currently down.
 * keyboard_check_pressed(key) Returns whether the key with the particular keycode was pressed since the last step.
 * keyboard_check_released(key) Returns whether the key with the particular keycode was released since the last step.
 * keyboard_check_direct(key) Returns whether the key with the particular keycode is pressed by checking the hardware directly. The result is independent of which application has focus. It allows for a few more checks. In particular you can use keycodes vk_lshift, vk_lcontrol, vk_lalt, vk_rshift, vk_rcontrol and vk_ralt to check whether the left or right shift, control or alt key is pressed.

Keycodes
All the above functions use Game Maker's virtual keycodes. (For more information on these, refer to the the keycode constants.) For example, here is a simple movement code:

{   if (keyboard_check(vk_left))  x -= 4; if (keyboard_check(vk_right)) x += 4; if (keyboard_check(vk_up))   y -= 4; if (keyboard_check(vk_down)) y += 4; }

Keyboard Variables
In addition to the above functions, the following helper variables can be used to capture keyboard interaction.


 * keyboard_lastkey Keycode of last key pressed. See below for keycode constants. You can change it, e.g. set it to 0 if you handled it.
 * keyboard_key Keycode of current key pressed (see below; 0 if none).
 * keyboard_lastchar Last character pressed (as string).
 * keyboard_string String containing the last at most 1024 characters typed. This string will only contain the printable characters typed. It also correctly responds to pressing the backspace key by erasing the last character.

Virtual Manipulation
The following routines can be used to manipulate the keyboard state:


 * keyboard_get_numlock Returns whether the numlock is set.
 * keyboard_set_numlock(on) Sets (true) or unsets (false) the numlock.
 * keyboard_key_press(key) Simulates a press of the key with the indicated keycode.
 * keyboard_key_release(key) Simulates a release of the key with the indicated keycode.

Key Mapping
Sometimes it is useful to map one key to another. For example you might want to allow the player to use both the arrow keys and the numpad keys. Rather than duplicating the actions you can map the numpad keys to the arrow keys. Also you might want to implement a mechanism in which the player can set the keys to use. For this the following functions are available:


 * keyboard_set_map(key1,key2) Maps the key with keycode key1 to key2.
 * keyboard_get_map(key) Returns the current mapping for key.
 * keyboard_unset_map Resets all keys to map to themselves.

Miscellaneous Functions
There are several other functions related to keyboard interaction.


 * keyboard_clear(key) Clears the state of the key. This means that it will no longer generate keyboard events until it starts repeating.
 * io_clear Clears all keyboard and mouse states.
 * io_handle Handle user io, updating keyboard and mouse status.
 * keyboard_wait Waits until the user presses a key on the keyboard.