Disable key pressing events when they repeat
Asked Answered
E

1

12

Is there any way to disable key pressing events when they are repeating?

I'm developing a game and when I press a key, the players goes into running mode and goes back to standing mode when the key is released. The problem is that with repeating keyPressEvent() events, there are automatically keyReleaseEvent() events, making the player immediately going back to standing mode after going into running mode, thus resetting the step (that I use for the running animation).

What should I do to cancel this?

Eurystheus answered 16/4, 2014 at 16:7 Comment(1)
Since you are developing a game you probably have a game loop. Does it work to check every iteration if a key is pressed or not? I am not sure if autorelease/press is a OS functionality (i guess). Just log your key presses and releases in a set-like container (press -> add it, release -> delete it) and check every loop iteration if the key is in the container.Creationism
S
22

Since you tagged your question with Qt, I assume your are using Qt key events - in that case, use QKeyEvent::isAutoRepeat() to check if the key event is a "real" key event or an autorepeat event.

Stack answered 16/4, 2014 at 18:37 Comment(2)
Thanks! it fixed my issue. I wanted to have a "hold" functionality for my keyboard events and it was not possible as in hold state, the pressed and released events were always triggered.Sweyn
I can hide all autorepeat key presses but how do I know which autorepeat key release is the last?Retral

© 2022 - 2024 — McMap. All rights reserved.