Search Unity

How to get AltGr-entered characters as input?

Discussion in 'Windows' started by JoeStrout, Apr 2, 2019.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,131
    I'm creating a custom code editor (for MiniScript). So I need to let the user type stuff, without using an UI.InputField. And since MiniScript (just like Python) uses [ ] and { } for lists and maps, it's important that the user be able to type those characters in particular.

    To get the user's typing, I'm using Input.inputString. Although (as documented) this works only for ASCII characters, on an American keyboard, at least it works fine for all ASCII characters. But on a European keyboard where [, ], {, and } are all key combinations using the AltGr key, these generate no inputString at all.

    I could possibly work around this by explicitly checking for these keys with Input.GetKey... except that we need this to work in versions 5.5 — 2017, and KeyCode.LeftCurlyBracket did not exist prior to 2018. And if you try to call GetKey("{"} anyway, it throws an ArgumentException. It appears that nobody at Unity noticed that curly braces were not supported until last year. :(

    But checking for these four characters is a hack anyway. Surely there's some other method of getting text input? What about non-ASCII characters, while we're at it? Isn't there some way to get any character the user can type, regardless of how many keys he has to press to type it? How do the built-in InputField and TextMeshInputField do it?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,131
    Well. Naturally, 5 minutes after posting, I found a solution: use Event.character in onGUI.

    Still open to better suggestions if you have any, but this appears to work.