Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Where is the Esc key press event?
Qt 6.12 LTS is out! See what's new in the release blog

Where is the Esc key press event?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 14.4k Views 2 Watching
  • 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
    #2

    Hi
    The combobox itself have an event filter to catch clicks on the
    viewport that is the dropdown list.
    Since esc on a "open" combox, closes the dropdown, its very likely
    that its keyevent handle it.

    you can try put a event filter on
    QAbstractItemView * QComboBox::view() const
    and see what comes in.
    They are called in reverse order of install so you should be called first.

    1 Reply Last reply
    1
    • V Offline
      V Offline
      Violet Giraffe
      wrote on last edited by
      #3

      @mrjj said in Where is the Esc key press event?:

      you can try put a event filter on
      QAbstractItemView * QComboBox::view() const
      and see what comes in.
      They are called in reverse order of install so you should be called first.

      Nope, it's not there, either.
      Isn't it weird that even QApplication::notify does not receive this event?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #4

        Any advice for me? Sorry to be annoying, but this is required for an important feature of my pet project. And even more importantly, I just don't see why it should be impossible to handle this key.

        mrjjM 1 Reply Last reply
        0
        • V Violet Giraffe

          Any advice for me? Sorry to be annoying, but this is required for an important feature of my pet project. And even more importantly, I just don't see why it should be impossible to handle this key.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Violet-Giraffe

          Waiting 3 days and then ask is not annoying :)

          I would go look at the combo box source to be sure its as expected and then i would try to installing an event filter on the qApp object and look for QEvent::KeyPress .

          Did you already try that ? I mean filter directly on Application?

          V 1 Reply Last reply
          0
          • mrjjM mrjj

            @Violet-Giraffe

            Waiting 3 days and then ask is not annoying :)

            I would go look at the combo box source to be sure its as expected and then i would try to installing an event filter on the qApp object and look for QEvent::KeyPress .

            Did you already try that ? I mean filter directly on Application?

            V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #6

            @mrjj said in Where is the Esc key press event?:

            Did you already try that ? I mean filter directly on Application?

            I have tried that just now, it also does not detect this event. Which is not surprising, given that I already tried QApplication::notify.

            Time to look at the QComboBox source, I guess :/ Not my favorite part of Qt development, sifting through heaps of unfamiliar code written in a style that I find hard to read :)

            JonBJ 1 Reply Last reply
            0
            • mranger90M Offline
              mranger90M Offline
              mranger90
              wrote on last edited by
              #7

              You might want to try installing the event filter on the the combobox->view() object.
              Using this I can get the esc key press event. The esc key release event shows up in the parent combo box.

              V 1 Reply Last reply
              0
              • V Violet Giraffe

                @mrjj said in Where is the Esc key press event?:

                Did you already try that ? I mean filter directly on Application?

                I have tried that just now, it also does not detect this event. Which is not surprising, given that I already tried QApplication::notify.

                Time to look at the QComboBox source, I guess :/ Not my favorite part of Qt development, sifting through heaps of unfamiliar code written in a style that I find hard to read :)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #8

                @Violet-Giraffe
                Purely, purely OOI, what is the important feature that your pet project would like to do when the user presses Esc to dismiss a combo? :)

                1 Reply Last reply
                0
                • mranger90M mranger90

                  You might want to try installing the event filter on the the combobox->view() object.
                  Using this I can get the esc key press event. The esc key release event shows up in the parent combo box.

                  V Offline
                  V Offline
                  Violet Giraffe
                  wrote on last edited by
                  #9

                  @mranger90 said in Where is the Esc key press event?:

                  You might want to try installing the event filter on the the combobox->view() object.
                  Using this I can get the esc key press event. The esc key release event shows up in the parent combo box.

                  Nothing. No press, no release.
                  I have installed the filter to the combobox object, to its view, and to its lineEdit. The filter looks like this:

                  if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
                  {
                  		QKeyEvent * keyEvent = dynamic_cast<QKeyEvent*>(e);
                  		qDebug() << keyEvent;
                  {
                  
                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Violet Giraffe
                    wrote on last edited by
                    #10

                    Tried the same in a new empty project, Esc is detected no problem there.
                    So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?

                    JonBJ 1 Reply Last reply
                    0
                    • V Violet Giraffe

                      Tried the same in a new empty project, Esc is detected no problem there.
                      So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #11

                      @Violet-Giraffe
                      https://stackoverflow.com/a/27607947/489865
                      ?

                      V 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Violet-Giraffe
                        https://stackoverflow.com/a/27607947/489865
                        ?

                        V Offline
                        V Offline
                        Violet Giraffe
                        wrote on last edited by
                        #12

                        @JNBarchan said in Where is the Esc key press event?:

                        @Violet-Giraffe
                        https://stackoverflow.com/a/27607947/489865
                        ?

                        Yes, that is exactly what I did. I already use it for catching another elusive event, so I know notfy() itself is working.

                        JonBJ 1 Reply Last reply
                        0
                        • V Violet Giraffe

                          @JNBarchan said in Where is the Esc key press event?:

                          @Violet-Giraffe
                          https://stackoverflow.com/a/27607947/489865
                          ?

                          Yes, that is exactly what I did. I already use it for catching another elusive event, so I know notfy() itself is working.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #13

                          @Violet-Giraffe
                          I meant (I could be way out, mind...) that you asked how the existing application code might have already done an override and grabbed away the ESC ?

                          So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?

                          V 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Violet-Giraffe
                            I meant (I could be way out, mind...) that you asked how the existing application code might have already done an override and grabbed away the ESC ?

                            So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?

                            V Offline
                            V Offline
                            Violet Giraffe
                            wrote on last edited by
                            #14

                            @JNBarchan said in Where is the Esc key press event?:

                            @Violet-Giraffe
                            I meant (I could be way out, mind...) that you asked how the existing application code might have already done an override and grabbed away the ESC ?

                            So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?

                            Oh! No, I have looked very closely at my notify() to make sure it doesn't do anything unexpected. It really does not:

                            inline bool notify(QObject * receiver, QEvent * e) override
                            {
                            		if (e->type() == QEvent::KeyPress)
                            		{
                            			QKeyEvent * keyEvent = static_cast<QKeyEvent*>(e);
                            			if (keyEvent->key() == Qt::Key_Tab)
                            			{
                            				// Handles Tab press....
                            				return true;
                            			}
                            		}
                            
                            		return QApplication::notify(receiver, e);
                            }
                            
                            1 Reply Last reply
                            0
                            • mranger90M Offline
                              mranger90M Offline
                              mranger90
                              wrote on last edited by
                              #15

                              Here's what worked for me. I was able to get the esc key event with the drop down active using this code....

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow),
                                  m_comboBox(nullptr)
                              {
                                  ui->setupUi(this);
                                  m_comboBox = ui->comboBox;
                                  m_comboBox->addItem("Summer");
                                  m_comboBox->addItem("Rose");
                                  m_comboBox->addItem("Gimpy");
                              
                                  m_comboBox->installEventFilter(this);
                                  m_comboBox->view()->installEventFilter(this);
                              
                              }
                              
                              MainWindow::~MainWindow()
                              {
                                  delete ui;
                              }
                              
                              
                              bool MainWindow::eventFilter(QObject *obj, QEvent *ev)
                              {
                                  if (obj == m_comboBox)
                                  {
                                      if (ev->type() == QEvent::KeyPress)
                                      {
                                          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                          qDebug() << "Key Press event for " << keyEvent->key();
                                      }
                                      if (ev->type() == QEvent::KeyRelease)
                                      {
                                          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                          qDebug() << "Key Release event for " << keyEvent->key();
                                      }
                                      return false;
                                  }
                                  else if (obj == m_comboBox->view())
                                  {
                                      if (ev->type() == QEvent::KeyPress)
                                      {
                                          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                          qDebug() << "View  Key Press event for " << keyEvent->key();
                                      }
                                      if (ev->type() == QEvent::KeyRelease)
                                      {
                                          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                          qDebug() << "View  Key Release event for " << keyEvent->key();
                                      }
                                      return false;
                                  }
                                  else
                                  {
                                      return QMainWindow::eventFilter(obj, ev);
                                  }
                              }
                              
                              
                              V 1 Reply Last reply
                              1
                              • mranger90M mranger90

                                Here's what worked for me. I was able to get the esc key event with the drop down active using this code....

                                MainWindow::MainWindow(QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::MainWindow),
                                    m_comboBox(nullptr)
                                {
                                    ui->setupUi(this);
                                    m_comboBox = ui->comboBox;
                                    m_comboBox->addItem("Summer");
                                    m_comboBox->addItem("Rose");
                                    m_comboBox->addItem("Gimpy");
                                
                                    m_comboBox->installEventFilter(this);
                                    m_comboBox->view()->installEventFilter(this);
                                
                                }
                                
                                MainWindow::~MainWindow()
                                {
                                    delete ui;
                                }
                                
                                
                                bool MainWindow::eventFilter(QObject *obj, QEvent *ev)
                                {
                                    if (obj == m_comboBox)
                                    {
                                        if (ev->type() == QEvent::KeyPress)
                                        {
                                            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                            qDebug() << "Key Press event for " << keyEvent->key();
                                        }
                                        if (ev->type() == QEvent::KeyRelease)
                                        {
                                            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                            qDebug() << "Key Release event for " << keyEvent->key();
                                        }
                                        return false;
                                    }
                                    else if (obj == m_comboBox->view())
                                    {
                                        if (ev->type() == QEvent::KeyPress)
                                        {
                                            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                            qDebug() << "View  Key Press event for " << keyEvent->key();
                                        }
                                        if (ev->type() == QEvent::KeyRelease)
                                        {
                                            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
                                            qDebug() << "View  Key Release event for " << keyEvent->key();
                                        }
                                        return false;
                                    }
                                    else
                                    {
                                        return QMainWindow::eventFilter(obj, ev);
                                    }
                                }
                                
                                
                                V Offline
                                V Offline
                                Violet Giraffe
                                wrote on last edited by
                                #16

                                @mranger90 said in Where is the Esc key press event?:

                                Here's what worked for me. I was able to get the esc key event with the drop down active using this code....

                                So I've tried exactly that already, and it doesn't work in my target application, while it does work in a new test application. I can't understand why.

                                Meanwhile, if I install the native event filter to QApplication, I can detect the Exc press there. But this is not a solution, implementing such a minute thing thrice (for each major desktop OS).

                                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
                                • Unsolved