Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
keycode <-> realcode <-> keycode — Gideros Forum

keycode <-> realcode <-> keycode

MoKaLuxMoKaLux Member
edited September 2021 in General questions
hope you are all doing fine :)
I would like to let the player choose the keys for playing, I have this:

How can I display the letter (here LEFT) instead of the constant 37?

Do I need to map myself all the keys? wouldn't that be problematic with different languages, pc or mac, ... ? Some code:
g_keyleft = KeyCode.LEFT
local textInputDialog = TextInputDialog.new("assign new key", "LEFT", g_keyleft, "Cancel", "OK")
local function onComplete(event)
	print(event.text, event.buttonIndex, event.buttonText)
end
textInputDialog:addEventListener(Event.COMPLETE, onComplete)
textInputDialog:show()
I will store each keys to variables (here g_keyleft)...
Thank you for your help :)
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • MoKaLuxMoKaLux Member
    edited September 2021
    or even better:
    1- the user presses the button to remap the left direction
    2- button blinks (or changes color, ...)
    3- user presses the new key
    4- game uses the new key
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • -- key codes are integer. we map to strings so that we can display key name easily
    local keyNames = {
      [KeyCode.BACK] = "back",
      [KeyCode.SEARCH] = "search",
      [KeyCode.MENU] = "menu",
      [KeyCode.CENTER] = "center",
      [KeyCode.SELECT] = "select",
      [KeyCode.START] = "start",
      [KeyCode.L1] = "L1",
      [KeyCode.R1] = "R1",
      [KeyCode.LEFT] = "left",
      [KeyCode.UP] = "up",
      [KeyCode.RIGHT] = "right",
      [KeyCode.DOWN] = "down",
      [KeyCode.X] = "x",
      [KeyCode.Y] = "y",
      [KeyCode.SPACE] = "space",
      [KeyCode.BACKSPACE] = "backspace",
      [KeyCode.SHIFT] = "shift",
    }
     
    local function onKeyDown(event)
      text1:setText("key down: "..(keyNames[event.keyCode] or "unknown"))
    end
    ???
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • E1e5enE1e5en Member
    edited September 2021 Accepted Answer
    I suppose you should write the input handler yourself without using the TextInputDialog, because it does not have an input processing event in the field (not described in the documentation).

    You can use utf8.char() to convert a key code to a character. Example:
    stage:addEventListener(Event.KEY_DOWN, function(event)
    	print("KeyCode: ".. tostring(event.keyCode))
    	print("Key:"..utf8.char(event.keyCode))
    end)
    For a "nice" display of the values of special characters (BackSpace, Space, Left, Right), your array is useful.
    stage:addEventListener(Event.KEY_DOWN, function(event)
    	if (keyNames[event.keyCode] or "unknown") ~= "unknown" then
    		print("KeyCode: ".. tostring(event.keyCode))
    		print("Key:"..keyNames[event.keyCode])
    	else
    		print("KeyCode: ".. tostring(event.keyCode))
    		print("Key:"..utf8.char(event.keyCode))
    	end
    end)
    Beginner game developer:https://e1e5en.itch.io, Google Play
  • MoKaLuxMoKaLux Member
    edited September 2021
    perfect E1e5en :)
    I also find this stage:addEventListener(Event.KEY_CHAR, onKeyChar) which is not documented :D
    local textc = TextField.new(nil, "key chars: ")
    textc:setScale(4)
    textc:setPosition(10, 130)
    stage:addChild(textc)
     
    local function onKeyChar(event)
    	textc:setText("key chars: "..event.text)
    end
     
    stage:addEventListener(Event.KEY_CHAR, onKeyChar)
    EDIT: further reading on keycode https://forum.gideros.rocks/discussion/7593/no-keycode-sent-for-some-keys-when-shift-is-being-pressed

    Likes: E1e5en

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • E1e5enE1e5en Member
    edited September 2021 Accepted Answer
    An example might come in handy :)

    By clicking on the mouse button, the editing mode is activated and you can drive in a key from the keyboard. There you can check which buttons can be assigned and which cannot.
    Project: link

    Likes: MoKaLux

    Beginner game developer:https://e1e5en.itch.io, Google Play
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.