Results 1 to 4 of 4

Thread: KeyPress and KeyRelease events not happening as they should

  1. #1
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default KeyPress and KeyRelease events not happening as they should

    Hello all,

    I made a custom event filter for my application and installed it on my instance of QApplication. However, I noticed some strange behavior. When I press a key (once and it doesnt get held down) I do get the KeyPress event (I check for auto repeat first and if its false I handle the event other wise let event processing continue and this is because if someone holds down a key I do not want it sending out multiple keypress events and keyrelease events), I do not receive a KeyRelease event until I press the key again at which I do not receive the KeyPress. Any Ideas of what I could be doing wrong?

    Below is my code that handles key events (I do some other checks above to make sure the widget in question or more specifcally the qobject in question has focus).
    I pass emsg (which is an internal struct that I use to propagate events to other modules), I am using windows 7 64 bit but compiling and linking with Qt in 32 bit.

    This is the use case of pressing a key once:
    Qt Code:
    1. //q is pressed and immediately released
    2. switch (event->type()) {
    3.  
    4. case QEvent::KeyPress:
    5. {
    6. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    7. if (!keyEvent->isAutoRepeat()) {
    8. //It falls in here fine the first go around but never gets to released
    9. emsg.wParam = keyEvent->key();
    10. emsg.lParam = keyEvent->modifiers();
    11. } else {
    12. keyEvent->ignore();
    13. return false;
    14. }
    15. break;
    16. }
    17. case QEvent::KeyRelease:
    18. {
    19. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    20. if (!keyEvent->isAutoRepeat()) {
    21. //it never gets here but if I press q again it falls in here and not in keypress
    22. emsg.wParam = keyEvent->key();
    23. emsg.lParam = keyEvent->modifiers();
    24. } else {
    25. keyEvent->ignore();
    26. return false;
    27. }
    28. break;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    SEGMENTATION FAULT: At who the hell knows where.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: KeyPress and KeyRelease events not happening as they should

    How did you check release event / press event is not being received. I mean did you have a break point, or qDebug()....

    Also are you trying to sniff the key, or eat(filter) the keys,In either cases return proper value (true/false).
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: KeyPress and KeyRelease events not happening as they should

    I had break points in each press and release. First time it was pressed i Received only the press and second time I pressed the same key I got the release event. My input manager returns true if it picked off the key event.
    SEGMENTATION FAULT: At who the hell knows where.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyPress and KeyRelease events not happening as they should

    Using breakpoints to trace key and mouse events can be misleading because of the changes in focus and active window that occur when the breakpoint is hit. Try simply using qDebug() statements instead so the flow of execution isn't interrupted by breakpoints and see what gets reported.

Similar Threads

  1. Display of the same dialog is not happening
    By kulsekarr in forum Newbie
    Replies: 6
    Last Post: 27th December 2012, 22:07
  2. keyrelease problem in qt
    By kinjalp in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2012, 11:30
  3. KeyPress and KeyRelease Events
    By sattu in forum Qt Programming
    Replies: 8
    Last Post: 30th September 2011, 18:23
  4. Handle KeyRelease or KeyPress on any row in QTableView
    By AbuYusuf in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2010, 20:04
  5. Problem with catching keyPress events ?
    By arbi in forum Qt Programming
    Replies: 12
    Last Post: 1st September 2008, 12:35

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.