Results 1 to 8 of 8

Thread: Keyboard auto repeat

  1. #1
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Keyboard auto repeat

    hi there,
    i need to disable autorepeat of keyboard events. This means that a single KeyPress event is generated for each key pressed, and a KeyRelease is shot only when a key is released on the keyboard.

    I didn't find anything userful about key autorepeat on the documentation, but some info about QKeyEvent::isAutoRepeat() but, like documentation say:
    Note that if the event is a multiple-key compressed event that is partly due to auto-repeat, this function could return either true or false indeterminately.
    sometimes it can't be reliable to use this funcion (and for my needs, it's quite bad)

    Any help is really appreciated.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keyboard auto repeat

    Quote Originally Posted by akiross View Post
    I didn't find anything userful about key autorepeat on the documentation, but some info about QKeyEvent::isAutoRepeat() but, like documentation say:

    Note that if the event is a multiple-key compressed event that is partly due to auto-repeat, this function could return either true or false indeterminately.
    sometimes it can't be reliable to use this function (and for my needs, it's quite bad)
    Noticed the "compressed event"??? It means that the unreliability occurs only when two conditions several key press are compressed into a single key event and in this case text() return a multi-character string. This can only happen on input widgets whose attribute Qt::WA_KeyCompression is set to true. Hopefully this attribute is off by default and is set to on only in text editing widgets (QTextEdit and QLineEdit for instance) If you use such widgets and can't afford this (highly improbable) lack of reliability the simplest solution is to disable key compression for them. It may make them a little slower but in most case will be unnoticeable and will let you do proper checks on key events...

    Hope this helps.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Keyboard auto repeat

    If all elses fail you can always set a flag on first press event and check the flag in each subsequent key press event. If it is set, simply don't handle the event. And of course you'll need to clear the flag in release event. Note that the flag can be a QSet and not boolean if you need to handle more than one key at a time.

  4. #4
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard auto repeat

    Yep! It helps Thanks i didn't notice it.

    Ok, at least now i can use that function to check... But i still prefer to choose if enabling key repeat (well, i'm doing a game, and i come with the libSDL experience, where by default key repeat is off but you can enable and modify it as your wish). To me, disabling it at all will result in a more straightforward code...

    ... But for now this is ok

    So, well, this is just mine sudgestion for qt developers: a function like QWidget::enableKeyRepeat(bool) would be even better

    Fullmetalcoder, thanks a lot!

    EDIT: Also thanks to wysota, the idea of using a set for multiple keypress is really nice! I used (but in C, without sets) to do something like char pressed[256];
    But sets are far better for this.
    Thanks again!

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Keyboard auto repeat

    If you're making a game, then you have a very simple solution... As you probably want your game to be stable independent of the computer it is running on, you should use a QTimer object (or a timer event) to synchronise your game loop. And if you do, you can simply check the key state there instead of relying on key press/release events. QSet might be helpfull here as well.

    Qt Code:
    1. QSet<Qt::Key> keysPressed;
    2. void W::keyPressEvent(QKeyEvent *ev){
    3. keysPressed += (Qt::Key)ev->key();
    4. }
    5. void W::keyReleaseEvent(QKeyEvent *ev){
    6. keysPressed -= (Qt::Key)ev->key();
    7. }
    8.  
    9. void W::timerEvent(QTimerEvent *ev){
    10. foreach(Qt::Key k, keysPressed){
    11. //...
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard auto repeat

    Mmh well actually i won't need the set in this particular game, i just liked the idea

    For this one i'll rely on events to handle user inputs. Main loop will be used only for drawing purpose. Actually, Qt is class based, so events are outside the main loop.
    In SDL context, events were put in an event queue and polled at every refresh, so a set would be wonderful there.

    But here i'm using a timer for refresh, events aren't syncronized with this timer because i'm keeping objects interact asyncronously: a physic engine will have its loop for integrate the simulation, GLwidget loop will have its loop for refreshing the screen, but the game under the hood isn't synced.

    Well, i'm just trying this approach, since it's the first time i do a game with Qt (and C++)... If i'll found that doesn't work good, i'll use the classic method

    Thanks anyway

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keyboard auto repeat

    Quote Originally Posted by akiross View Post
    Actually, Qt is class based, so events are outside the main loop.
    In Qt input events are fed by the system (X11, Window$, ...) to the event loop of the GUI thread (== main loop) which dispatch them to proper receivers.
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #8
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard auto repeat

    yes yes, i know

    I intended to say that usually you do it by hand (in SDL it's like)
    Qt Code:
    1. while (1) { // main loop
    2. while (SDL_PollEvent(&ev)) {
    3. switch (ev.type) {
    4. // Handle here events
    5. }
    6. // Here game drawing loop
    7. }
    To copy to clipboard, switch view to plain text mode 

    While in qt the event polling is done by Qt; I'm saying that in Qt events are handled outside main loop because a Press method will be called on Press event.

    To me, this is not only more readable and clear to write in code, but leaves also implementation freedom. If Qt a day will prefer to map directly system's event to my application, in this way there is no need to pass by an event queue, with SDL method it doesn't.

    (well, it's quite ugly as example but i hope you got )

Similar Threads

  1. Grab keyboard events in Windows
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2007, 19:56
  2. Auto update module
    By munna in forum General Discussion
    Replies: 3
    Last Post: 26th September 2006, 14:52
  3. Replies: 2
    Last Post: 24th July 2006, 18:36
  4. Keyboard Handling
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 5th July 2006, 13:25
  5. Fast Keyboard, help !
    By Alex63 in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2006, 18:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.