Virtual Keys And Virtual Keyboards


When developing games for touchscreen devices, it's often important to get keyboard input from the user. When using GameMaker Studio 2 this can be done in one of two ways:

What you use will depend very much on the game and what you need, but in general you would use virtual keys when you want to have onscreen buttons for player control or for menus, while you'd use the virtual keyboard when you need player input, like getting a name or setting a value. The sections below outline the uses and functions associated with each of these input options.

In order for your games to work properly on mobile devices with a touch screen, you may need to be able to assign areas of the screen to respond to certain things when they are touched by the user. This is done by assigning a "virtual Key" to an internally mapped standard keyboard key and then using the keyboard events that they generate to control your application. It is worth noting that these "virtual keys" are mapped to the screen position rather than room position and so the x/y values are absolute, based on the display size. This means that you don't need to worry about the use of views or the relative room coordinates, and can simply draw your key sprites and define your virtual keys in the Draw GUI Event of an object.

NOTE: These functions will not work on the Windows, Ubuntu (Linux), nor the MacOS, target platforms using a touch screen, although they will recieve mouse clicks as "touches".

The available virtual key functions are:


If you require text or numeric input in your game on a touchscreen device, then you can use the virtual keyboard functions provided to use the device OS keyboard. These functions will call up or hide the OS keyboard and accept input from it, however it will not trigger the regular keyboard events, but rather update the keyboard_string variable with the user input (this is due to the fact that the virtual keyboard can input whole words and not just single characters).

Opening or closing the virtual keyboard will generate System Asynchronous Events, in which the async_load DS map will be populated with the following key/value pairs:

  • "event_type" - the type of system event received, which will be "virtual keyboard status" for virtual keyboards.

  • "screen_height" - the height of the virtual keyboard (in pixels). This will be 0 if the keyboard is invisible.

  • "keyboard_status" - the current status of the keyboard, returned as one of the following strings:

    • "hiding"
    • "hidden"
    • "showing"
    • "visible"

The available virtual keyboard functions are:

  1. keyboard_virtual_show
  2. keyboard_virtual_hide
  3. keyboard_virtual_status
  4. keyboard_virtual_height
NOTE: These functions are only valid for the Android (including Amazon Fire, FireTV, and AndroidTV), iOS, and UWP target platforms.