[PyQt] About keyPressEvent

Andreas Pakulat apaku at gmx.de
Thu Aug 23 09:54:47 BST 2007


On 23.08.07 00:54:59, "Gustavo A. Dí­az" wrote:
> I did this time:
> 
> def keyPressEvent(self, event):
>              if event.key() == QtCore.Qt.Key_Escape:
>                  self.hide()
>            
>         if event.key() == QtCore.Qt.Key_Alt + QtCore.Qt.Key_Escape:
>             self.hide()
> 
> Esc key only, hides the app.
> Alt + Esc does not, it just close the app.
> 
> What is wrong?

You didn't understand how a keypress translates into a key press event.
A keypress (when you push down Alt) is directly creating a key press
event, which is going to be delivered. On the next key press (the Esc
key) you'll get another key press event. So the 2nd if is never true,
also because event.key() always returns 1 keycode, not the sum of 2.

What you probably want is

if event.key() == QtCore.Qt.Key_Escape and (event.modifiers() &
QtCore.Qt.AltModifier):
...

Andreas

-- 
You will be the victim of a bizarre joke.


More information about the PyQt mailing list