GameMaker: Input field (semi-advanced)

There are multiple cases where you may want to let user input text in a familiar way, keeping things intact with system settings and allowing minor tweaks like repositioning the text cursor. Such include consoles, chatboxes, input fields, and other. This example covers these needs with fairly short (less than 70 lines total) and fairly understandable code. It includes the following features:

  • Keeps track of input text on window focus loss. If you've tried to make an input box before, you may be similar with that unpleasant default behaviour of keyboard_string in GameMaker.
  • Allows to reposition cursor (left / right / home / end keys)
  • Allows to use Delete key to erase characters
  • Can be easily modified to support other keys and shortcuts

Example includes actual input field object, and sample list drawer, to which typed text is sent upon press of Enter.

Download GMK

Related posts:

21 thoughts on “GameMaker: Input field (semi-advanced)

  1. Hey,
    I have been messing around a bit with your work but I a afraid of messing with this piece of code because I don’t understand how it works. I think i am getting close to completing it so that you can change the lines after pressing enter.
    I would appreciate your help.

    // actual input:
    if (keyboard_string != filltext && keyboard_string != “”) {
    var n, l, t;
    t = keyboard_string
    l = string_length(t)
    n = 1
    // find where actual input starts
    while ((n 0 && caret > 0) {
    caret -= c
    text = string_delete(tex, caret[0] + 1, c)
    c = l – n + 1
    if (c > 0) {
    text = string_insert(string_copy(t, n, c), text, caret + 1)
    caret += c

    • Instead of messing with that code, it would probably be a better idea to change the string being edited to that of the new line, and store the current one for display on-screen. There are a few complete text field examples on marketplace if I remember right.

  2. Any updated location for this file? I’m browsing different text input methods, and i ran into this one (by recommendation on the forums)

  3. Do you know a code I can add so if I were to say “game_restart’ in the input box, the game would find that function I added and restart the game? :/

    I like the style! No scripts, just some coding.

  4. Hello,
    Thanks for the input field, really helped me with my project.
    My mother language and the language that will be used in it is Hebrew, and it’s a right-to-left language.
    I added support for the Hebrew characters but the text comes out mirrored.
    Can you help me please?

    • שלום אופק!!

      נתקלתי בתגובתך לאתר בנוגע לכתיבת עברית בגיימייקר
      (קלט עברית וכד’)

      ?האם הצלחת לפתור את הבעיה

      אודה לך מאוד אם תשתף אותי בקוד מסוים שמאפשר קלט-פלט בעברית ללא אפקט מראה.

      תודה (:


    • For HTML5 it would generally be a wiser decision to write an extension that would place an actual <input> element over the game instead of trying to write a keyboard handler yourself (which will not be able to consider every possible keyboard layout anyway)

  5. Pretty good and short for script but you cannot go up or down nor erase once you have created a new line so multiple lines are not yet supported.

    • Correct, this is currently geared towards single-line fields. Adding multi-line support would be possible, but would require increasing complexity of code quite a bit. Will try to look into that sometime.

  6. Just saw this coming from yoyo forum, I was wondering if you could help a newbie like me to GM and suggest a way to integrate this code into an existing prj. I’d like to ask the user his name when the game finished (i.e. lives = 0) so I can write it up in a file and use it in the leaderboard at startup screen. I was using the get_string() function but I don’t like the ugly window pop-up at all, your solution it’s much more elegant from a look’n’feel point of view.

    I’ve tried to create the object (the same that’s in your project) with the same code, and used a create_instance inside my code in place of the get_string but it just doesn’t do anything… I guess I’m too much of a newbie to GM though.

    any help/suggestion is much appreciated.


  7. I was excited to see this text input example because as the previous commenter said it’s something that’s serious lacking from GameMaker. That said, I’m disappointed it doesn’t invoke the virtual keyboard on mobile devices when used with HTML5. That’s not the fault of your example, but rather a continued inefficiency of GameMaker. Nevertheless, thanks for all your hard work!

    • Indeed, while bringing up virtual keyboard is possible, it is completely out of scope of this example.
      It’s not even just about the GameMaker – it’s also about how HTML5 works: system keyboard will not pop up unless the user has tapped an according element (input/textarea). Thus, to bring up the keyboard, you have to trick the user into tapping such an input field. And that, in turn, means that all further event capturing must be done via that element.
      That, in combination with current lack of a proper JS API for interacting with engine, makes achieving of such task fairly problematic.

  8. Thanks, if it wasnt for your sniplet Game Maker would have been uninstalled already. Seems silly something that is used to make HTML5 applications has no form fields.

Leave a 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.