Results 1 to 11 of 11

Thread: Space key doesn't trigger keyPressEvent

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Space key doesn't trigger keyPressEvent

    Hello there!

    The main object of my application is a QMainWindow where I grabKeyboard() in the constructor to handle all keyboard input in the keyPressEvent of the main window. Additionally, there are a bunch of QActions in a QMenuBar that listen and react to key presses directly. All this works wonderfully, except for the space key. When I press any key that is not bound to an action, the keyPressEvent should be called, but in the case of the space key it does not get called. I did not bind the space key to any action. Does anyone know why and how to fix it?

    Thanks
    Cruz

  2. #2
    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: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by Cruz View Post
    Hello there!

    The main object of my application is a QMainWindow where I grabKeyboard() in the constructor to handle all keyboard input
    Never do that.

    in the keyPressEvent of the main window. Additionally, there are a bunch of QActions in a QMenuBar that listen and react to key presses directly. All this works wonderfully, except for the space key. When I press any key that is not bound to an action, the keyPressEvent should be called, but in the case of the space key it does not get called. I did not bind the space key to any action. Does anyone know why and how to fix it?
    Implement proper key handling using focus and keyPressEvent() handlers in proper places.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    No, that neither explains where the problem is, nor does it offer a possible solution. It would only complicate my application by heaps.

  4. #4
    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: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by Cruz View Post
    No, that neither explains where the problem is, nor does it offer a possible solution. It would only complicate my application by heaps.
    grabKeyboard() causes all key events to reach your widget, regardless whether they are destined for it or not. You should not require it unless you have misdesigned your key event handling. So fixing your design is the first step to make your application working. Until you do that it is not possible to even start looking for the problem in any organized way using mechanisms Qt provides to debug your events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    All key presses are destined for the main window. That's exactly what I want to have and that's what grabKeyboard() does. It's a legitimate, Qt supported way to achieve this. I'm sure there are easier ways to debug this problem than to refactor the entire application to a more complicated form.

    I can for example remove everything from the QMainWindow and see if it reacts to the space key with the "proper" way to handle keyboard input (i.e. no grabKeyboard()). Yes it does. Interestingly, even with grabKeyboard() the space key works fine.

    And now that I readded all components piece by piece, I did come across a QAction that was tied to the space key. I didn't find it at first, because "space" was spelled with a lower case. :/ My bad.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    Why don't you use a global event filter instead of the grab keyboard hack?

    Cheers,
    _

  7. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    That's a good question. By the time when I wrote this application, I was a Qt beginner and event filtering was not within my horizon. The grabKeyboard() hack solved all my problems with one line so I was content with it (and I still am). Now that you brought it up, I looked into event filtering again. Yes, I suppose I could install an event filter on the QMainWindow and intercept all keyboard events that I'm interested in. Right now, the keyboard handling is in one central place, namely the keyPressEvent() of the main window, which I think is a nice place for it. Moving that to a separate filter object is something I would not find more beautiful. I suppose I could use the QMainWindow as an event filter for itself, in which case I would merely have to change the keyPressEvent() to an event filter function and then register the window as an event filter for itself instead of grabKeyboard(). Hm, does that really make things any easier or more beautiful?

    Oh and most importantly, with the event filter, would the key strokes for the QActions be intercepted? Because they combine nicely with the grabKeyboard() method.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    I would even consider installing the event filter on the QApplication object, all events pass through it.

    And as long as you don't filter them out they'll still reach whatever object they were sent to.

    Cheers,
    _

  9. #9
    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: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by Cruz View Post
    Yes, I suppose I could install an event filter on the QMainWindow and intercept all keyboard events that I'm interested in.
    That would be exactly equivalent of just reimplementing keyPressEvent for the main window. As long as child widgets do not handle key events themselves, they will be forwarded to the parent widget until they reach your main window where you will handle them. No need for any grabKeyboard() hacks.

    And regarding event filtering, I was also going to suggest installing the event filter on the application object or even subclassing QApplication and reimplementing notify() however I think all these solutions are hacks (in this particular situation) compared to proper event handling which I'm sure can easily be done without having to "complicate your application by heaps". Worst case scenario, you'd have to iterate over widgets and make sure they don't accept focus and then have your main object central widget accept focus and reimplement its keyPressEvent handler. All that can be done with 3-4 lines of code or by a quick one-time script run.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by wysota View Post
    As long as child widgets do not handle key events themselves...
    I don't know why you assume you have full information about my application, but to set this straight, the application contains widgets that do their own key event handling in undesired ways. For example, QSliders handle the arrow keys when they have focus. But the arrow keys have a different function in my program and it's important that the arrow keys do what they do no matter where the focus is. I would have to subclass QSlider or filter events or do whatever acrobatics is necessary to stop them from handling keys I need for something else. This goes for a number of widgets, all of which would require individual attention.

    Then there are also lovely cases where more than one widget needs to do something in response to a single key press, no matter where the focus is. As far as I can tell this is also not something that "proper" key handling is prepared for, because the widget that has the focus and handles the event, consumes the event too. So I would have to "hack" something to circumvent the default event handling mechanism and to forward the event to multiple widgets. Just because there are predefined Qt ways to accomplish this, it doesn't make it any less complex and cumbersome.

    The grabKeyboard() hack solves it all with one line and puts all keyboard handling in one place. It's easy to find, to understand, and to maintain.

  11. #11
    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: Space key doesn't trigger keyPressEvent

    Quote Originally Posted by Cruz View Post
    The grabKeyboard() hack solves it all with one line and puts all keyboard handling in one place. It's easy to find, to understand, and to maintain.
    I still think this is a hack. The sort of "offcial" way of providing your own event handling is to reimplement QCoreApplication::notify().

    If grabKeyboard() works for you then that's fine. However if you are only after modifying some keys globally then using either of the two mentioned solutions (notify() or application event filter) is a more clean approach without getting you into a God Object design anti-pattern. Simply delegate the "custom" behaviours to a dedicated place and leave everything else to the default mechanism.

    E.g.:

    Qt Code:
    1. bool XXX::eventFilter(QObject *object, QEvent *event) {
    2. if(event->type() != Qt::KeyPressEvent) return false;
    3. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    4. if(keyEvent->key() == Qt::Key_Left && object != myMainWindow) {
    5. myMainWindow->keyPressEvent(keyEvent); // or do whatever else you'd like, e.g. event->ignore() letting it propagate until the main window
    6. return true; // eat the left arrow key event
    7. }
    8. return false;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 20th September 2014 at 23:19. Reason: typo
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    Cruz (20th September 2014)

Similar Threads

  1. keyPressEvent doesn't work
    By 8Observer8 in forum Newbie
    Replies: 8
    Last Post: 27th August 2014, 06:29
  2. QTableView empty space or too little space.
    By davemar in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2009, 17:00
  3. Replies: 1
    Last Post: 26th July 2009, 16:08
  4. QComboBox as a trigger
    By ape in forum Newbie
    Replies: 8
    Last Post: 4th February 2008, 09:57
  5. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 9th March 2006, 00:11

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.