GameMaker: Android hardware button handling

Standard set of 4 Android hardware buttons

One of common questions about GameMaker: Studio include "How do I detect presses of hardware buttons (Home, Menu, Back, Search)?".
Despite of common assumptions, usage of these in GameMaker: Studio is pretty straight-forward - mentioned button events are automatically mapped to according keyboard button events. So, handling these is as easy as adding standard keyboard events.
More information about each follows,

Menu

"Menu" button is mapped to keyboard key "M" (index 77, ord("M")). It's worth noting that if you are running your game from inside of GameMaker: Studio (and thus using YoYoRunner) this button may not work at all times, opening runner menu or even popping up a keyboard.

Back

"Back/return" button (which you probably know as a curved arrow pointing left) is mapped to Backspace (index 8, vk_backspace). Fairly intuitive, and does not have any oddities.

Search

"Search" button is mapped to Control key (index 17, vk_control). Honestly, I fail to see the relevance of two, but that's not in scope of this article. A thing that you should also keep in mind is that not all devices have this button (especially android 4.x tablets), so you should not use it as the only method of input for any critically important in-game action.

Home

I'm not sure why do people even ask whether it's possible to bind an action to "Home" button. As you may guess, events related to this button are handled by OS itself (normally minimizing active application and opening home screen), so it's not possible to do much about it. You weren't planning to trap the player inside your game anyway, right?

Remarks

Despite of mapping, as of version 1.1.917 hardware buttons still do not change keyboard_lastkey / keyboard_lastchar variables, so it's not recommended to use two in combination..

I hope this was useful.

Related posts:

9 thoughts on “GameMaker: Android hardware button handling

    • You could try doing like

      var n = 0;
      for (var i = 2; i < 256; i++) if (keyboard_check(i)) {
      draw_text(5, 5 + n * 20, string(i));
      n += 1;
      }

      and see whether the volume buttons emit any key presses, though I think you'd to write need an extension to disable their default functions.

      Pretty sure it works in 4.4, as of writing this post I had a device with Android 2.x, and later used a device with Android 6, and it worked on both, so that's a safe interpolation.

      What do you mean by "the date"? date_get_current_time()?

    • You would want to use os_is_paused to detect the moment when the user minimizes the app (via Home button or not) and save the game at that time.

      • Hi, os_is_paused- don,t work cause when statement is reached game is paused and rest of the code will run after game unpause.

  1. I actually did want to trap the player in one game…

    It was a small game with shapes following various paths, and tapping them made them play tones or animal noises. It was made for my 1 year old son, and he constantly hits those hardware buttons and closes the game and then whines at me because it’s gone.

    I had seen another Android game for children that made the Home button not work, and the only way to exit was to tap all 4 corners of the screen. I had planned to put something kind of similar into my game until I found out that it was impossible with Game Maker.

    Usually a terrible choice, but in certain circumstances it would be helpful!

  2. Naz drovie,

    I ‘m a beginning programmer writing games for Android with GMStudio.
    First I tried Win 8, but it was too difficult to work with Visual Studio as well as GameMaker. I got fed waiting for the Microsoft Helpline, so while I was waiting for them to respond to call, I passed the time by creating an internet radio station – mostly old 60s stuff
    Your site is excellent – really useful and I’ve bookmarked it for future reference.
    Best wishes,

    Mick.

Leave a Reply to Krial Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.