Key GML Codes
For keyboard interaction, the following variables and functions exist:
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.
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.
To check whether a particular key or mouse button is pressed you can use the following functions. This is in particular useful when 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.
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.
The following constants for virtual keycodes exist:
vk_nokey keycode representing that no key is pressed
vk_anykey keycode representing that any key is pressed
vk_left keycode for left arrow key
vk_right keycode for right arrow key
vk_up keycode for up arrow key
vk_down keycode for down arrow key
vk_enter enter key
vk_escape escape key
vk_space space key
vk_shift shift key
vk_control control key
vk_alt alt key
vk_backspace backspace key
vk_tab tab key
vk_home home key
vk_end end key
vk_delete delete key
vk_insert insert key
vk_pageup pageup key
vk_pagedown pagedown key
vk_pause pause/break key
vk_printscreen printscreen/sysrq key
vk_f1 ... vk_f12 keycodes for the function keys F1 to F12
vk_numpad0 ... vk_numpad9 number keys on the numeric keypad
vk_multiply multiply key on the numeric keypad
vk_divide divide key on the numeric keypad
vk_add add key on the numeric keypad
vk_subtract subtract key on the numeric keypad
vk_decimal decimal dot keys on the numeric keypad
For the letter keys use for example ord('A'). (The capital letters.) For the digit keys use for example ord('5') to get the <5> key. The following constants can only be used in keyboard_check_direct:
vk_lshift left shift key
vk_lcontrol left control key
vk_lalt left alt key
vk_rshift right shift key
vk_rcontrol right control key
vk_ralt right alt key
There are some additional 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 till the user presses a key on the keyboard.
D&D Codes
Keyboard events
When the player presses a key, a keyboard event happens for all instances of all objects. There is a different event for each key. In the menu you can pick the key for which you want to define the keyboard event and next drag actions there. Clearly, only a few objects need events for only a few keys. You get an event in every step as long as the player keeps the key depressed. There are two special keyboard events. One is called <No key>. This event happens in each step when no key is pressed. The second one is called <Any key> and happens whatever key is pressed. When the player presses multiple keys, the events for all the keys pressed happen. Note that the keys on the numeric keypad only produce the corresponding events when <NumLock> is pressed.
Key press events
This event is similar to the keyboard event but it happens only once when the key is pressed, rather than continuously. This is useful when you want an action to happen only once.
Key release eventsThis event is similar to the keyboard event but it happens only once when the key is released, rather than continuously.
Hope this helps