YoYo Games Wiki

GML Functions: The Keyboard

From YoYoGames Wiki

This article (or section) may need to be wikified to meet The YoYo Games Wiki quality standards.
Please help improve this article, especially its categories, and wiki-links.

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:

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:

All the functions above use virtual keycodes. For more information about these, see the keycode constants.

For example, assume you have an object that the user can control with the arrow keys you can put the following piece of code in the step event of the object:

 {
   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;
 }

Of course it is a lot easier to simply put this in the keyboard events.

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.