how to detect what key is pressed by the player

Monkey Forums/Monkey Programming/how to detect what key is pressed by the player

Raul(Posted 2014) [#1]
I have a very big issue. I dont know how to detect what key is pressed by the player.

I need this in order to create an 'enter player name' field. i know is simple..


dawlane(Posted 2014) [#2]
Use getchar it's in the documentation with an example.


V. Lehtinen(Posted 2014) [#3]
Hey Raul,

Input fields are possible to make with the GetChar() function: http://www.monkey-x.com/docs/html/Modules_mojo.input.html#GetChar

Hope this helps!


ImmutableOctet(SKNG)(Posted 2014) [#4]
You probably want the 'GetChar' command; also see 'PeekChar'. That will give you the key pressed by the user, and if you write it like the demo, it will work with repeating characters when holding down a button. You'll need to detect what character the user writes, though. You'll mainly want to look for unwanted characters, and of course 'CHAR_BACKSPACE' for removing the last character of the string.

Here's my own modified version of the example from the documentation:


EDIT: Added a few configuration options, a few optimizations, and added some extra comments.


Playniax(Posted 2014) [#5]
If you are using Ignition X for this project you can use the input box: iGuiInput

Here 2 examples:

example 1
example 2


Raul(Posted 2014) [#6]
got it.

in 3 hours a local Game Jam organized by ubisoft in my town will be finished. i will post here my submision :P


Sensei(Posted 2015) [#7]
So, I've been playing with keyboard input on my android build of my game.

The problem I've found so far is that ASCII chars do work fine using GetChars(). However, the ones I'm interested in, ie Control, Arrow keys etc, don't work.
Simply put, my bluetooth keyboard connected to my Nexus 10 tablet doesn't pick any of them up.
I've tried KeyHit(Key_UP), KeyDown(KEY_UP), char = CHAR_UP ( char being Local char:Int = GetChar() ), yet char = 32 works (space), so I know I've done it right. I've even tried char = 65576 which is the code returned by keyboardtest.monkey in the bananas example. Again, no modifier keys, nor arrows work.

Anyone have any luck trying this out?

I'm using Monkey V80c and I've noticed in keycodes.monkey, they still seem wrong:

'non-ascii
Const CHAR_PAGEUP=KEY_PAGEUP | $10000
Const CHAR_PAGEDOWN=KEY_PAGEDOWN | $10000
Const CHAR_END=KEY_END | $10000
Const CHAR_HOME=KEY_HOME | $10000
Const CHAR_LEFT=KEY_LEFT | $10000
Const CHAR_UP=KEY_UP | $10000
Const CHAR_RIGHT=KEY_RIGHT | $10000
Const CHAR_DOWN=KEY_DOWN | $10000
Const CHAR_INSERT=KEY_INSERT | $10000