[PyQt] KeyPress events and numerical keys

David Cortesi davecortesi at gmail.com
Wed Nov 13 16:18:35 GMT 2013


> In keyPressEvent() I check the key against a list of configuraed keys to
> trigger one or another action. When you push a numerical key+shift, since
> you receive the symbol and the modifier (not the number plus the modifier,
> which is the defined key binding) the correspondant action can't be
> triggered.
>

Looking at your code I have two suggestions.

First, write yourself a small debugging routine to show you exactly what
key events you receive. (Here is one you might copy:
https://github.com/tallforasmurf/PPQT/blob/master/pqMsgs.py#L383) Put a
call to this in your keyPressEvent method and observe what events it
receives. This will be very educational, or at least, it was for me.

Then, I found it useful to combine the modifier and key values into single
integers. If you study the list of key values and modifier values (
http://qt-project.org/doc/qt-5.0/qtcore/qt.html#Key-enum) you see that
their binary values can be combined with code such as this:

CTL_1 = Qt.ControlModifier | Qt.Key_1

With some reorganization of your code, you could make a dictionary using
constants such as that for the key, and the corresponding object's method
itself for the value, and just call the dictionary entry directly:

mod_key = int(event.modifiers()) | int(event.key())
if mod_key in self.the_dictionary :
    the_dictionary[mod_key]() # do the action
else: event.ignore()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20131113/82853f41/attachment.html>


More information about the PyQt mailing list