[Kde-games-devel] keyboard delay on keyPressEvent

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Sun Feb 29 19:59:31 CET 2004


> thanks for help, just one question. Autorepeat in my case generates
> KeyPress and KeyRelease events in cycle. So it means that in kasteroids
> case, it sets rotateLeft (for example) true and false with every
> autorepeat event ?

Yes, for a key which is pressed, held, then released, you will get first a 
keyPress-event for the press, then potentially several keyPress-events for 
autorepeat, then finally a keyRelease-event. The first keyPress-event sets 
the variable to true, the following (autorepeat) events effectively don't do 
anything, since they simply keep setting the variable to true. The final 
keyRelease-event sets the variable to false. Effectively, this filters out 
the keyPress-events generated by autorepeat.

> the truth is, I didn't yet understand exactly, what this code:
>
> KKey key(event);
>      Action a = Invalid;
>      QMap<Action, KAction*>::Iterator it = keycodes.begin();
>      for (; it != keycodes.end(); ++it)
>      {
>         if ( (*it)->shortcut().contains(key) )
>         {
>             a = it.key();
>             break;
>         }
>      }
>
> does exactly, and why is it doing that what it is doing :)
> It's transforming Action to KAction or something simmilar.

For a first go, you can simply replace this code with:

if (event->key () == Qt::KeyLeft) {
	// left-arrow was pressed/released
} else if ...

This should work just fine. However, this way, the keys are statically coded 
and can't be configured.
What the above code is all about, is to cope with dynamic/configurable key 
bindings. The idea here is to first set up an action "rotateLeft", which can 
be easily configured with KDEs standard key-configuration dialog. Now, 
usually KActions are set up to trigger some slot (on keyPress, not 
keyRelease), which is not what you want for a game (due to the autorepeat 
problem). Instead you intercept the individual keypresses and keyreleases and 
then try to figure out, whether the key(-combination) that was pressed 
corresponds to the "rotateLeft"-action. The code you cite tries to map the 
keypress that was intercepted to the corresponding action.
See also
http://cvs.sourceforge.net/viewcvs.py/taxipilot/taxipilot/taxipilot/taxipilotapp.cpp?rev=1.28&view=auto
(TaxipilotApp:eventFilter) for a similar mechanism.

Thomas



More information about the kde-games-devel mailing list