View Full Version : How catch key press and key release for entire application

1st June 2010, 14:23
Is it possible to catch key up and key down on application level meaning before they are handled by the widgets? I want to be able to press a key for a while and then release it regardless of which widget that has the focus. Is there a generic way to do this, I am currently trying on Win32 and I tested inhereting keyPressed and keyReleased in my QMainWindow class and it works but as soon as a Widget has focus for instance if an element i selected in a list view the keyPressed event does not get to the MainWindow, the keyReleased event does however for some reason.

1st June 2010, 14:25
You might want to try an event filter.

1st June 2010, 15:46
You can search for flick charm example in Qt Labs... it also used event filters.
It will give you an idea how to stack events and perform required action.

1st June 2010, 15:53
I saw some replies on this and I have now tried the event filter but I get the exact same result. keyReleased event is passed as an event but keyPressed is not. I always get the keyReleased but not keyPressed. My only guess is that the keyPressed is taken by the QTreeView which currently has focus however I have also tried to add an event filter to the class that contains the QTreeView but that did not help. Do I have to inherit the QTreeView in order to intercept the keyPressed event with the eventfilter or is there another way to prevent the QTreeView from stealing my keypresses. This issue does not seem to exist on Symbian, only Win32.

1st June 2010, 21:53
I misunderstood the eventFilter approach and installed it on my class instead of the TreeView. I think I get how it works now. I can recommend this:
post. I just missed the first line, where the filter was installed, in the post which was very important.