Results 1 to 9 of 9

Thread: KeyPress and KeyRelease Events

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default KeyPress and KeyRelease Events

    Hi Everyone,

    To see how various keys behave in the context of Qt when pressed and released, I added 2 VIRTUAL FUNCTIONS to my MainWindow Class and implemented them in the following way:

    Qt Code:
    1. void MainWindow::keyPressEvent ( QKeyEvent * event )
    2. {
    3. qDebug("Came here,,,, Key Pressed");
    4. }
    5.  
    6. void MainWindow::keyReleaseEvent(QKeyEvent *event)
    7. {
    8. qDebug("Came here,,,, Key Released");
    9. }
    To copy to clipboard, switch view to plain text mode 


    What I observed is that, whenever I pressed any key, I would get both the qDebug statements as expected. But in the case of following four keys, I get only the qDebug of the keyReleaseEvent method:
    Tab key, LeftArrow key, RightArrow key, SpaceBar
    That means, for these keys only release event happens. I am really confused . Can anyone please explain why it is so?


    With Regards,
    Sattu

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: KeyPress and KeyRelease Events

    I have both events for those keys (Qt 4.7.2 + windows 7). Here is my code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class MyWindow: public QMainWindow
    5. {
    6. protected:
    7. virtual void keyPressEvent(QKeyEvent * event);
    8. virtual void keyReleaseEvent(QKeyEvent * event);
    9. };
    10.  
    11. void MyWindow::keyPressEvent(QKeyEvent *)
    12. {
    13. qDebug() << "key pressed";
    14. }
    15.  
    16. void MyWindow::keyReleaseEvent(QKeyEvent *)
    17. {
    18. qDebug() << "key released";
    19. }
    20.  
    21. int main(int argc, char * argv[])
    22. {
    23. QApplication a(argc, argv);
    24. MyWindow w;
    25. w.show();
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    make sure you have your widget focused.

  3. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: KeyPress and KeyRelease Events

    Quote Originally Posted by mentalmushroom View Post
    Qt Code:
    1. void MyWindow::keyPressEvent(QKeyEvent *)
    2. {
    3. qDebug() << "key pressed";
    4. }
    5.  
    6. void MyWindow::keyReleaseEvent(QKeyEvent *)
    7. {
    8. qDebug() << "key released";
    9. }
    10.  
    11. int main(int argc, char * argv[])
    12. {
    13. QApplication a(argc, argv);
    14. MyWindow w;
    15. w.show();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    make sure you have your widget focused.
    Hi Mushroom,
    Do one thing. Put 2 PushButtons on your Mainwindow.ui and see if you get the same result for the 4 keys I had mentioned.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: KeyPress and KeyRelease Events

    well, if you add controls to your window, keys like tab, arrow or space will be used to change focus or press the button etc, so these events are processed by the window children.

  5. #5
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: KeyPress and KeyRelease Events

    Quote Originally Posted by mentalmushroom View Post
    well, if you add controls to your window, keys like tab, arrow or space will be used to change focus or press the button etc, so these events are processed by the window children.
    Well, I understood you partially. I mean, if I add any controls, I get the KeyRelease Event for the keys I mentioned, but not KeyPress. How come? Can you explain a bit more?

  6. #6
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: KeyPress and KeyRelease Events

    i think, only key press events for those keys have the special meaning, so this is why only key press is "eaten". you may notice you change the focus when you press tab, not when you release it. if you wish to process those events installEventFilter may probably help in that.

  7. The following user says thank you to mentalmushroom for this useful post:

    sattu (30th September 2011)

  8. #7
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: KeyPress and KeyRelease Events

    Quote Originally Posted by mentalmushroom View Post
    i think, only key press events for those keys have the special meaning, so this is why only key press is "eaten". you may notice you change the focus when you press tab, not when you release it. if you wish to process those events installEventFilter may probably help in that.
    Yes, I do want to process those events, But I have never been through "installEventFilter" concept. So, it's a normal pre-defined method or something like that?
    Give me a minimum idea so that I immediately start with it.

  9. #8
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: KeyPress and KeyRelease Events

    I'd suggest you to read the documentation for installEventFilter function. I think, it explains much better than I can do.

  10. The following user says thank you to mentalmushroom for this useful post:

    sattu (30th September 2011)

  11. #9
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: KeyPress and KeyRelease Events

    Ok Thanks, Will go through the documentation.

Similar Threads

  1. QListWidget keypress capture ?
    By tonnot in forum Qt Programming
    Replies: 5
    Last Post: 29th September 2011, 20:07
  2. Handle KeyRelease or KeyPress on any row in QTableView
    By AbuYusuf in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2010, 20:04
  3. fn+f1 keypress detect
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 16:21
  4. Problem with catching keyPress events ?
    By arbi in forum Qt Programming
    Replies: 12
    Last Post: 1st September 2008, 12:35
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 14:12

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.