How to catch Shift+Ctrl+key in keyPressEvent?

Hello,

I’ve reimplemented a QTextEdit’s keyPressEvent:


class MyTextEdit(QTextEdit):
   def __init__(self, *args):
   QTextEdit.__init__(self, *args)

   keyPressedSignal = pyqtSignal(int, bool, bool)

   def keyPressEvent(self, event):
      shift = False
      ctrl = False
      if (event.modifiers() & Qt.ShiftModifier):
         shift = True
      if (event.modifiers() & Qt.ControlModifier):
         ctrl = True
      self.keyPressedSignal.emit(event.key(), shift, ctrl)

It catches either Shift or Ctrl but not both. Any idea how to catch both Shift and Ctrl modifiers? Thank you.

PS. This code doesn’t work only in Windows, in Linux it works fine, both Shift and Ctrl are caught.

This maybe another thread where you get your own solution within 30 mins from starting it :wink:
But in case you really want help from others, do you mind telling (preferable in the thread Title, but else in the beginning of the first Post) what programming language it is all about? This would avoid people not knowing that language from reading it and attrackt the people who are gurus in that language. Better for you I think (and for us also).

No luck this time, still trying to figure it out…

I prefer Python but would like a suggestion in any language, that’s why I didn’t include it. Should have probably mentioned it must be using Qt (and Python/C++/Java/anything)

Yes, every bit of information to make it easy to others to understand what you are doing in what context is usefull. You may be thinking of those allready for days and night on end to the amount that make you forget that other languages, etc. do exist, but that does not mean that others can read your brains.

I am still wating for somebody needing help with Algol 60 :wink:

Hi,

I just was rethinking if it would not be more useful if you are creating a QAction item and using the setShortcut Qt 4.6: QAction Class Reference. QAction allows the key sequence constructed from string e.g. “Ctrl+N”. I think this is more straight forward than intercepting the key event yourself and then emitting the signal and this is exact what QAction is designed for.

Hope this helps

linuxoidoz wrote:

> It catches either Shift or Ctrl but not both. Any idea how to catch
> both Shift and Ctrl modifiers? Thank you.

Are you quite sure? event.modifiers() should return the same set of
modifiers when handling the same event. And from the looks of it, if you
press Shift-Ctrl-A it should set both shift as well as ctrl to true.

Debugging session?


Ruurd

Also is it ok according to the API to call event.modifiers() more than once or are you supposed to save the result if you want it more than once?

i.e.

modifiers = event.modifiers()
if (modifiers & Qt.ShiftModifier)

if (modifiers & Qt.ControlModifier)

Monex,

I’ve actually made a dictionary of all keyboard keys and don’t need a Ctrl+Shift+key any more. But QAction with a shortcut is a good idea, I will probably need it for implementing the editor shortcuts.

R.F. Pels,

Yes, I’m sure. In Linux it catches both Ctrl and Shift, but on Windows it only catches Shift. It can catch Ctrl, but if you press Shift+Ctrl, then it only catches Shift. Stupid vindoze. Anyway, I don’t need to use that anymore and now it all works fine on both Linux and windows.

ken_yap,

Looks like it was OK to call them > 1 in Linux, but not in Windows. I was saving the modifier states and just passing bool variable depending on the received modifier state.