Keyboard Input

When dealing with the keyboard in GameMaker Studio 2 you have a variety of functions that can be used to recognise different keyboard states like pressed or released. There are also some that store all the key-presses as a string or that can tell you what the last key pressed was, as well as others that allow you to clear the keyboard state completely.

NOTE: These functions are designed for Windows/Mac/Ubuntu desktop platforms only. You may find some of the built-in variables and constants aren't valid on other platforms and many of the functions won't work on mobiles.

Each input character from a key (or multiple keys) is defined by its UTF8 code, which is a numerical value. This value can be retrieved for any character using the ord() function but, GameMaker Studio 2 also has a series of constants for the most used keyboard special keys and a special functions. Typically you'd use a combination of ord() with the keyboard_check*() functions, something like this:

if keyboard_check(ord("A"))
    {
    hspeed = -5;
    }

So, the above will check the "A" key and if it's being pressed then it'll set the horizontal speed of the object to -5. Note, that using ord() in this way will only function correctly if the input string is only one character in length and is a number from 0 to 9 or a capitalised Roman character from A to Z. The function ord() will return a full UTF8 value, but the keyboard_check*() functions will only detect A - Z and 0 - 9. But what if you want to use the arrow keys? Or if you want to modify an action using the "shift" key? Well, for that GameMaker Studio 2 has a series of vk_ constants (vk_ stands for virtual key) that you can use in place of ord():

 

Constant Description
vk_nokey keycode representing that no key is pressed
vk_anykey keycode representing that any key is pressed
vk_left keycode for the left arrow key
vk_right keycode for the right arrow key
vk_up keycode for the up arrow key
vk_down keycode for the down arrow key
vk_enter enter key
vk_escape escape key
vk_space space key
vk_shift either of the shift keys
vk_control either of the control keys
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 keycode 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

 

The following constants can only be used with keyboard_check_direct():

 

Constant Description
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

 

The following is a small example of how to use the vk_ constants:

if keyboard_check_pressed(vk_tab)
    {
    instance_create_layer(x, y, "Controllers", obj_Menu);
    }

The above code will detect if the "Tab" key is pressed and create an instance of object "obj_Menu" if it is.

If you need to check for a key character that is not 0 - 9, A - Z or one of the VK constants, then you should be checking one of the keyboard_* variables, like keyboard_lastchar for example:

var _key = keyboard_lastchar;
if ord(_key) == ord("ç")
    {
    show_debug_message("ç key pressed");
    }

The following functions are available for use when working with a keyboard:

NOTE: These functions will not work when using an on-screen Virtual Keyboard.

 

There are also some functions that can be used to simulate keypresses:

 

 

Finally, the following variables are built-in to GameMaker Studio 2 and can be accessed to get information about the keyboard state and input:

NOTE: When using the Virtual Keyboard, only the keyboard_string variable will be updated with the keyboard input.