Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Prevent Qt application from reacting to Tab and Alt keys
QtWS25 Last Chance

Prevent Qt application from reacting to Tab and Alt keys

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 10.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #7

    hi
    Its send as a modifier
    void myClass::keyPressEvent(QKeyEvent *e)
    {
    if ((e->key()==Qt::Key_Return) && (e->modifiers()==Qt::AltModifier))
    doSomething();
    }

    So its not really listed as a key.

    pcsaganP 1 Reply Last reply
    0
    • mrjjM mrjj

      hi
      Its send as a modifier
      void myClass::keyPressEvent(QKeyEvent *e)
      {
      if ((e->key()==Qt::Key_Return) && (e->modifiers()==Qt::AltModifier))
      doSomething();
      }

      So its not really listed as a key.

      pcsaganP Offline
      pcsaganP Offline
      pcsagan
      wrote on last edited by
      #8

      @mrjj

      Okay, so is there any way to override that behavior? If not, then it would indeed seem that the Qt framework assumes the user would never want to override the behavior of the Alt key, which is quite odd and unfortunate. In any low level application it's very easy to treat all keys on the keyboard as equal, but of course it's preferable to use Qt over writing custom Windows message pumps.

      A mosquito was heard to complain, that chemists had poisoned his brain, the cause of his sorrow was paradichlorodiphenyltrichloroethane.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #9

        What platform are you running your code on ?

        You made me doubt so I've double checked. On OS X at least, there's indeed key press and release events generated for the modifiers (AFAIK, something new)

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • pcsaganP Offline
          pcsaganP Offline
          pcsagan
          wrote on last edited by
          #10

          :P

          I'm running on Windows 10 x86-64bit with Python 3.4 and using PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x64 bindings.

          A mosquito was heard to complain, that chemists had poisoned his brain, the cause of his sorrow was paradichlorodiphenyltrichloroethane.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #11

            Can you check with a basic C++ project if you have the same results ? Just to be sure PyQt is innocent.

            #include <QApplication>
            #include <QWidget>
            #include <QtDebug>
            
            class MyWidget : public QWidget
            {
            public:
                explicit MyWidget(QWidget *parent = nullptr)
                    : QWidget(parent)
                {}
            
                void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE
                {
                    qDebug() << "Pressed" << (Qt::Key) event->key();
                }
            
                void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
                {
                    qDebug() << "Released" << (Qt::Key) event->key();
                }
            
            };
            
            int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);
                MyWidget mw;
                mw.show();
                return app.exec();
            }
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            kshegunovK 1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #12

              hi
              Test on win 10, Qt 5.5, empty mainwindow

              void keyReleaseEvent(QKeyEvent* e) {
              qDebug() << "release" << (e->key() == Qt::Key_Alt);
              if ( e->modifiers() == Qt::AltModifier)
              qDebug() << "ALT up";
              }

              void keyPressEvent(QKeyEvent* e) {
              if ( e->modifiers() == Qt::AltModifier)
              qDebug() << "ALT down" << (e->key() == Qt::Key_Alt);
              }

              Give me "ALT up/down" with key Qt::Key_Alt.
              So seem it does send it also as key. (on windows)

              UPDATE:
              It gives me ALT as modifier on down , but only as key when released. ( make sense)

              1 Reply Last reply
              2
              • SGaistS SGaist

                Can you check with a basic C++ project if you have the same results ? Just to be sure PyQt is innocent.

                #include <QApplication>
                #include <QWidget>
                #include <QtDebug>
                
                class MyWidget : public QWidget
                {
                public:
                    explicit MyWidget(QWidget *parent = nullptr)
                        : QWidget(parent)
                    {}
                
                    void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE
                    {
                        qDebug() << "Pressed" << (Qt::Key) event->key();
                    }
                
                    void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
                    {
                        qDebug() << "Released" << (Qt::Key) event->key();
                    }
                
                };
                
                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                    MyWidget mw;
                    mw.show();
                    return app.exec();
                }
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #13

                @SGaist
                The key events are also available on Linux (Debian, 4.x kernel with Qt 5.5).
                I always thought that modifiers are sent only as such, not as regular keys, but I guess this had been changed at some point ...

                @pcsagan,
                Seeing that @mrjj also receives presses/releases correctly, I'd guess there's something specific to the python bindings, but you should run the example code to make sure.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • pcsaganP Offline
                  pcsaganP Offline
                  pcsagan
                  wrote on last edited by
                  #14

                  Hey guys, I ran this test as requested and it works the way the C++ implementation works, and which also happens to be the functionality I desired.

                  from PyQt5 import QtCore, QtWidgets
                  import sys
                  
                  class Widget(QtWidgets.QWidget):
                      def __init__(self, parent=None):
                          super(Widget, self).__init__(parent)
                  
                      def keyPressEvent(self, event):
                          if event.key() == QtCore.Qt.Key_Alt:
                              print('Alt pressed!')
                  
                      def keyReleaseEvent(self, event):
                          if event.key() == QtCore.Qt.Key_Alt:
                              print('Alt released!')
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      widget = Widget()
                      widget.show()
                      sys.exit(app.exec_())
                  

                  What's interesting is that this behavior changes when you create a menu in the menubar via:

                  self.fileMenu = self.menuBar().addMenu('&File')
                  self.newConsoleAct = QtWidgets.QAction(QtGui.QIcon('assets/icons/console.png'), '&New Console', self, shortcut='Ctrl+N', statusTip='Create a new client console', triggered=self.newConsole)
                  self.fileMenu.addAction(self.newConsoleAct)
                  

                  The code above will capture the focus and set it to the menu bar, and I don't know how to prevent this from happening. What's even more interesting is that when you add a QtWidgets.QMdiArea the behavior changes to what I originally described where the 'in between' key press/release events are somehow consumed, and you need to press and release Alt twice to see both pressed and release events trigger, and it still sets focus to the menu bar.

                  I assume that if you guys were to create a test application that had a menu bar and MDI area you'd see the same change in behavior of the keyPressEvent() and keyReleaseEvent(). I'm hoping I eventually discover some kind of policy flag that disables this behavior, but in the mean time I think I'll just use non-modifier keys for my scene's custom controls. I appreciate the help from you guys regardless. Thanks!

                  A mosquito was heard to complain, that chemists had poisoned his brain, the cause of his sorrow was paradichlorodiphenyltrichloroethane.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    &File < there you have an accelerator thus the the Key_Alt + Key_F combo must be detected.
                    Same goes for newConsoleAct where you have both an accelerator and a shortcut which must also be detected.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    pcsaganP 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      &File < there you have an accelerator thus the the Key_Alt + Key_F combo must be detected.
                      Same goes for newConsoleAct where you have both an accelerator and a shortcut which must also be detected.

                      pcsaganP Offline
                      pcsaganP Offline
                      pcsagan
                      wrote on last edited by
                      #16

                      @SGaist

                      Yeah, that's the functionality I'm trying to disable.

                      A mosquito was heard to complain, that chemists had poisoned his brain, the cause of his sorrow was paradichlorodiphenyltrichloroethane.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        If you want to disable that, why not avoid accelerators ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • pcsaganP Offline
                          pcsaganP Offline
                          pcsagan
                          wrote on last edited by
                          #18

                          The thing is I actually really like the default behavior of the Qt framework, and it's remarkable at providing easy access to convenient and common functionality straight out of the box. I don't want to avoid accelerators because I want to provide keyboard shortcuts for the various things users can do in my application.

                          I was hoping that in my QGraphicsScene I could have the Ctrl key toggle between ScrollHandDrag and RubberBandDrag drag modes while still retaining Ctrl+<Key> shortcut functionality in the application (I simply wasn't going to assign the Alt key to any accelerators). I also wanted to toggle displaying textual information above items in the scene while the user has Alt pressed down (like Diablo 2 when items are on the ground, you only see the item text while Alt is held in a pressed state).

                          So in my application with a menu bar, MDI central widget, and graphics scene MDI child window, I effectively want to be able to press Ctrl + drag the mouse to select many items, then hold Alt to verify they're the ones I wanted selected by reading the text above them, then press Ctrl+R to rotate all selected items by 90 degrees.

                          When I said "that's the functionality I want to disable" I was referring to the mechanism that automatically captures the Alt key and reacts to it. I want to be in complete control of how the application responds to the Alt key. To be fair, and as I stated earlier, I can just use 'Q' and 'A' and 'Z' to achieve the functionality I want, but it's less intuitive than using the modifier keys to change the behavior of the scene (i.e. change selection mode and display item text).

                          A mosquito was heard to complain, that chemists had poisoned his brain, the cause of his sorrow was paradichlorodiphenyltrichloroethane.

                          kshegunovK 1 Reply Last reply
                          0
                          • pcsaganP pcsagan

                            The thing is I actually really like the default behavior of the Qt framework, and it's remarkable at providing easy access to convenient and common functionality straight out of the box. I don't want to avoid accelerators because I want to provide keyboard shortcuts for the various things users can do in my application.

                            I was hoping that in my QGraphicsScene I could have the Ctrl key toggle between ScrollHandDrag and RubberBandDrag drag modes while still retaining Ctrl+<Key> shortcut functionality in the application (I simply wasn't going to assign the Alt key to any accelerators). I also wanted to toggle displaying textual information above items in the scene while the user has Alt pressed down (like Diablo 2 when items are on the ground, you only see the item text while Alt is held in a pressed state).

                            So in my application with a menu bar, MDI central widget, and graphics scene MDI child window, I effectively want to be able to press Ctrl + drag the mouse to select many items, then hold Alt to verify they're the ones I wanted selected by reading the text above them, then press Ctrl+R to rotate all selected items by 90 degrees.

                            When I said "that's the functionality I want to disable" I was referring to the mechanism that automatically captures the Alt key and reacts to it. I want to be in complete control of how the application responds to the Alt key. To be fair, and as I stated earlier, I can just use 'Q' and 'A' and 'Z' to achieve the functionality I want, but it's less intuitive than using the modifier keys to change the behavior of the scene (i.e. change selection mode and display item text).

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #19

                            @pcsagan
                            Maybe you could use void qt_set_sequence_auto_mnemonic(bool enable) to disable the menu bar from intercepting the alt key presses?

                            PS. I forgot you were working with python. I don't know a way to call that function from pyton as it most probably is not available in the bindings ...

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved