So, I’ve been having some issues with a bit of practice code. Here it is:
local function adjustText(event) local key = event.keyName local phase = event.phase if (phase == "down") then text = display.newText(foreGroup, key, display.contentCenterX, 250, native.systemFont, 20) end if (phase == "up") then display.remove(text) end end Runtime:addEventListener("key", adjustText)
“text” is a local variable declared earlier in the program.
The idea is that, when someone pressed down a key, text will appear that shows which key is being pressed down. When they stop pressing it, the text disappears. The code I made successfully does this, as long as only one key is being pressed at a time. If two or more keys are being pressed, however, only one goes text object goes away. I assume this has something to do with how event objects work, and that the event listener is unable to send the “up” phase event if another event occurs before then. Is there a way to get around this?
Ideally, I would know how to have the following effects, based on what I need at the time, but help with even one would be great!
- Once a key is pressed, further key presses are ignored until that key has been lifted. Only one input is accepted at a time.
- Once a key is pressed, the first key is treated as completed and the new key is focused on. In this example, pressing a second key after the first one, without releasing the first one, would act as if you had pressed the first one, released it, then immediately pressed the second key.
- Both key inputs are handled simultaneously. They would both show up, and both could be interacted with and accounted for. Upon releasing the keys, all of them would go away.
Any help would be appreciated!