Results 1 to 8 of 8

Thread: Keyboard auto repeat

Hybrid View

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

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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 

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

    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

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

    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

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

    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
  •  
Qt is a trademark of The Qt Company.