Key Event is Being Interrupted Before Completion

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!

Give this a try: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/keys.zip

This example only allows one key press at a time, all other keypresses are ignored till the current one is done.

I changed the output a bit to show what is going on better.

It also handles backspace to delete the last char in the string.

You should be able to write other variations based on this.

local cx = display.contentCenterX local cy = display.contentCenterY local keyTracking = {} local function count() local sum = 0 for k,v in pairs( keyTracking ) do sum = sum + 1 end return sum end local group = display.newGroup() local output = display.newText( group, "", cx, cy, native.systemFont, 40 ) local function key( event ) local phase = event.phase local keyName = event.keyName print(keyName, phase, count()) if( phase == "down" ) then if( count() == 0 ) then if( keyName == "deleteBack" ) then local len = string.len( output.text ) if( len == 1 ) then output.text = "" elseif( len \> 1 ) then output.text = string.sub( output.text, 1, len-1 ) end else output.text = output.text .. keyName end end keyTracking[keyName] = true elseif( phase == "up" ) then keyTracking[keyName] = nil end end Runtime:addEventListener("key", key)

Thanks for the reply! It really helped a lot. I managed to figure out all the cases I wanted to with your help!

Give this a try: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/keys.zip

This example only allows one key press at a time, all other keypresses are ignored till the current one is done.

I changed the output a bit to show what is going on better.

It also handles backspace to delete the last char in the string.

You should be able to write other variations based on this.

local cx = display.contentCenterX local cy = display.contentCenterY local keyTracking = {} local function count() local sum = 0 for k,v in pairs( keyTracking ) do sum = sum + 1 end return sum end local group = display.newGroup() local output = display.newText( group, "", cx, cy, native.systemFont, 40 ) local function key( event ) local phase = event.phase local keyName = event.keyName print(keyName, phase, count()) if( phase == "down" ) then if( count() == 0 ) then if( keyName == "deleteBack" ) then local len = string.len( output.text ) if( len == 1 ) then output.text = "" elseif( len \> 1 ) then output.text = string.sub( output.text, 1, len-1 ) end else output.text = output.text .. keyName end end keyTracking[keyName] = true elseif( phase == "up" ) then keyTracking[keyName] = nil end end Runtime:addEventListener("key", key)

Thanks for the reply! It really helped a lot. I managed to figure out all the cases I wanted to with your help!