onKeyEvent "down" event fires multiple times when key is held down

Hi there,

I am checking for a key press (in this case, the letter “a”) - everything goes well except the event is firing repeatedly in the simulator. 

I am checking for event.phase == “down” before running my function, so I’m not sure why the “down” event is being fired when I hold the button down.

Is this a Mac OSX thing? A simulator thing? Any help is appreciated, thanks!

Nick

if (event.keyName == "a") then print(event.phase) if (event.phase == "down") then -- this stuff fires repeatedly end end

Assuming you’re calling this as a Runtime listener, this seems like normal behavior. Your listener is checking every frame for whether the “a” key is in the “down” phase or not. 

UPDATE:

It occurs to me, that this might not be your preferred behavior. You can throw in a flag that gets set depending on key state, so you only see the key called once. Something like:

keyState = 1 if (event.keyName == "a") then print(event.phase) if (event.phase == "down") and keyState == 1 then keyState = 2 -- this stuff should only fire once. end if (event.phase == "up") and keyState == 2 then keyState = 1 -- this stuff should only fire once. end end

YMMV HTH

Thanks for the help, Alex, I appreciate. Your solution is nice, I implemented something similar using a flag as a temporary workaround - 

But is this really the expected behavior? The documentation states that “Key events occur when a keyboard key or gamepad/joystick button has been pressed down or released.” I would expect that the event would fire once when the key is pressed and once when it is released, not continuously. What do you think?

I’m looking at my other projects with keymapping and they aren’t working the way they did 6 months ago. So I have no clue what is going on.

That said, I remember a forum thread talking about the importance of “return true” in hardware button handling. I’ll try to dig it up.

Thanks for the help Alex, I appreciate it - let me know if you find anything!

Does anyone from Corona have any insight on whether or not the behavior I’m experiencing is expected?

Assuming you’re calling this as a Runtime listener, this seems like normal behavior. Your listener is checking every frame for whether the “a” key is in the “down” phase or not. 

UPDATE:

It occurs to me, that this might not be your preferred behavior. You can throw in a flag that gets set depending on key state, so you only see the key called once. Something like:

keyState = 1 if (event.keyName == "a") then print(event.phase) if (event.phase == "down") and keyState == 1 then keyState = 2 -- this stuff should only fire once. end if (event.phase == "up") and keyState == 2 then keyState = 1 -- this stuff should only fire once. end end

YMMV HTH

Thanks for the help, Alex, I appreciate. Your solution is nice, I implemented something similar using a flag as a temporary workaround - 

But is this really the expected behavior? The documentation states that “Key events occur when a keyboard key or gamepad/joystick button has been pressed down or released.” I would expect that the event would fire once when the key is pressed and once when it is released, not continuously. What do you think?

I’m looking at my other projects with keymapping and they aren’t working the way they did 6 months ago. So I have no clue what is going on.

That said, I remember a forum thread talking about the importance of “return true” in hardware button handling. I’ll try to dig it up.

Thanks for the help Alex, I appreciate it - let me know if you find anything!

Does anyone from Corona have any insight on whether or not the behavior I’m experiencing is expected?