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. using QKeyEvents

using QKeyEvents

Scheduled Pinned Locked Moved Solved General and Desktop
40 Posts 4 Posters 4.7k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 7 May 2020, 18:03 last edited by
    #4

    Hi,

    It's the other way around, you need to install KeyPress in your QMainWindow.

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

    M 1 Reply Last reply 7 May 2020, 18:21
    1
    • S SGaist
      7 May 2020, 18:03

      Hi,

      It's the other way around, you need to install KeyPress in your QMainWindow.

      M Offline
      M Offline
      mzimmers
      wrote on 7 May 2020, 18:21 last edited by
      #5

      @SGaist not using QMainWindow; using QWidget.

      class Widget : public QWidget
      {
          Q_OBJECT
      ...
      

      Same thing, though?

      And by "install KeyPress" I assume you mean more than create an object of that class, since I'm already doing that.

          m_keyPress = new KeyPress(this);
          connect(m_keyPress, &KeyPress::keyEvent, this, &Widget::handleKeyEvent);
          m_keyPress->installEventFilter(this);
      

      I'm missing a step here, aren't I?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 7 May 2020, 18:25 last edited by SGaist 5 Jul 2020, 18:26
        #6

        You're not missing a step, you are doing one in the wrong direction. The object on the left of installEventFilter is the one that is going to be monitored so just call

        installEventFilter(m_keyPress);
        

        See the installEventFilter documentation.

        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
        1
        • M Offline
          M Offline
          mzimmers
          wrote on 7 May 2020, 18:49 last edited by
          #7

          OK...made that change; still not seeing what I expect.

              // create and connect a KeyPress object.
              m_keyPress = new KeyPress(this);
              connect(m_keyPress, &KeyPress::keyEvent, this, &Widget::handleKeyEvent);
              installEventFilter(m_keyPress);
          

          Here's my event routine:

          void KeyPress::keyPressEvent(QKeyEvent *ev)
          {
              int key;
          
              key = ev->key();
              if (key == Qt::Key_Alt)
              {
                  emit keyEvent(QEvent::KeyPress, key);
              }
          }
          

          I'm never hitting a breakpoint in there.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 7 May 2020, 19:17 last edited by
            #8

            Because an event filter re-implements the eventFilter method and does all there. It does not replace a class original methods.

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

            M 1 Reply Last reply 7 May 2020, 19:41
            0
            • S SGaist
              7 May 2020, 19:17

              Because an event filter re-implements the eventFilter method and does all there. It does not replace a class original methods.

              M Offline
              M Offline
              mzimmers
              wrote on 7 May 2020, 19:41 last edited by
              #9

              @SGaist said in using QKeyEvents:

              Because an event filter re-implements the eventFilter method and does all there. It does not replace a class original methods.

              So, which object needs the eventFilter() method? I've tried adding to both the Widget and the KeyPress -- both compile and run, but neither trigger a breakpoint.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 7 May 2020, 19:53 last edited by
                #10

                Did you check the example I linked to ?

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

                M 1 Reply Last reply 7 May 2020, 21:26
                1
                • S SGaist
                  7 May 2020, 19:53

                  Did you check the example I linked to ?

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 7 May 2020, 21:26 last edited by
                  #11

                  @SGaist you're referring to the KeyPressEater class in the QObject::installEventFilter() documentation, right?

                  Its behavior isn't the same as my KeyPress class, but I can worry about that later. I still don't see why I'm not hitting a breakpoint.

                  bool KeyPress::eventFilter(QObject *obj, QEvent *ev)
                  {
                      bool rc = false;
                  
                      if (ev->type() == QEvent::KeyPress)
                      {
                          if (obj == this)
                          {
                              rc = true;
                              qDebug() << ev;
                          }
                      }
                      return rc;
                  }
                  

                  My breakpoint is on my inner "if" and it's never reached.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 7 May 2020, 21:37 last edited by
                    #12

                    Because as is, your filter will never be the target of a KeyPress event.
                    The value of obj will be one of the object you installed your filter on.

                    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
                    • M Offline
                      M Offline
                      mzimmers
                      wrote on 7 May 2020, 21:42 last edited by
                      #13

                      I understand your 2nd statement, but not the first. I install the filter on my Widget object. Is a QWidget incapable of accepting KeyPress events?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mzimmers
                        wrote on 7 May 2020, 23:49 last edited by
                        #14

                        Well, I got it partly working -- the problem was that I was deriving my KeyPress class from QWidget instead of QObject.

                        Now, though, my filter only seems to be trapping "meta keys." Control, alt, etc. all work, but regular keys like A, B, C don't. It appears that these keys generate a ShortcutOverride event. Does this make sense to you?

                        Thanks...

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mzimmers
                          wrote on 8 May 2020, 15:47 last edited by
                          #15

                          My filter seems to be doing what I want (when two keys are held down, I make a button visible).

                              if (C_pressed && D_pressed)
                              {
                                  ui->pushButtonNics->setVisible(true);
                              }
                              else
                              {
                                  ui->pushButtonNics->setVisible(false);
                              }
                          

                          But when I click on that button, my clicked slot isn't reached. I thought maybe holding down the keys was blocking the button push, but my other buttons work fine. My button is enabled when visible (I checked in the debugger). Any ideas why it's not generating the signal?

                          Thanks...

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 8 May 2020, 15:54 last edited by
                            #16

                            You are forgetting to call the base class implementation.

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

                            M 1 Reply Last reply 8 May 2020, 16:09
                            0
                            • S SGaist
                              8 May 2020, 15:54

                              You are forgetting to call the base class implementation.

                              M Offline
                              M Offline
                              mzimmers
                              wrote on 8 May 2020, 16:09 last edited by
                              #17

                              @SGaist I hadn't forgotten; I just hadn't gotten to that yet. But shouldn't I hit this slot anyway?

                              void Widget::on_pushButtonNics_clicked()
                              {
                                  qDebug() << "Nics pressed.";
                              }
                              
                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 8 May 2020, 18:00 last edited by
                                #18

                                The last implementation I saw does not call it at all which means that all events are filtered out.

                                To check, comment out the filter installation and see if it does work correctly.

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

                                M 1 Reply Last reply 8 May 2020, 18:06
                                0
                                • S SGaist
                                  8 May 2020, 18:00

                                  The last implementation I saw does not call it at all which means that all events are filtered out.

                                  To check, comment out the filter installation and see if it does work correctly.

                                  M Offline
                                  M Offline
                                  mzimmers
                                  wrote on 8 May 2020, 18:06 last edited by
                                  #19

                                  @SGaist let's make sure we're talking about the same thing. Here's where I create the filter:

                                      // create and connect a KeyPress object.
                                      m_keyPress = new KeyPress(this);
                                      connect(m_keyPress, &KeyPress::keyEvent, this, &Widget::handleKeyEvent);
                                      installEventFilter(m_keyPress);
                                  
                                      // hide the button for the NICs window.
                                  //    ui->pushButtonNics->setVisible(false);
                                  

                                  You'll notice that the last line is commented out. And, it works. So it would seem that it has something to do with the filter...?

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 8 May 2020, 18:12 last edited by
                                    #20

                                    Can you provide a minimal compilable example that shows the behaviour ?
                                    Si au can check on my side what is going on.

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

                                    M 1 Reply Last reply 8 May 2020, 18:16
                                    0
                                    • S SGaist
                                      8 May 2020, 18:12

                                      Can you provide a minimal compilable example that shows the behaviour ?
                                      Si au can check on my side what is going on.

                                      M Offline
                                      M Offline
                                      mzimmers
                                      wrote on 8 May 2020, 18:16 last edited by
                                      #21

                                      @SGaist

                                      keypress.h

                                      #ifndef KEYPRESS_H
                                      #define KEYPRESS_H
                                      
                                      #include <QWidget>
                                      #include <QtGui>
                                      
                                      class KeyPress : public QObject
                                      {
                                          Q_OBJECT
                                      public:
                                          explicit KeyPress(QWidget *parent = nullptr);
                                      protected:
                                          bool eventFilter(QObject *obj, QEvent *ev) override;
                                      
                                      signals:
                                          void keyEvent(QEvent event, int key);
                                      };
                                      
                                      #endif // KEYPRESS_H
                                      

                                      keypress.cpp

                                      #include "keypress.h"
                                      
                                      KeyPress::KeyPress(QWidget *parent) : QObject (parent)
                                      {
                                      }
                                      
                                      bool KeyPress::eventFilter(QObject *obj, QEvent *ev)
                                      {
                                          Q_UNUSED(obj)
                                      
                                          bool rc = false;
                                          int key;
                                          QKeyEvent *qke;
                                      
                                          if (ev->type() == QEvent::ShortcutOverride || ev->type() == QEvent::KeyRelease)
                                          {
                                              qke = static_cast<QKeyEvent *>(ev);
                                              key = qke->key();
                                      
                                              // don't bother signaling when the key isn't meaningful to us.
                                              if (key == 'c' || key == 'C' || key == 'd' || key == 'D')
                                              {
                                                  emit keyEvent(*ev, key);
                                              }
                                              rc = true;
                                          }
                                          else
                                          {
                                              rc = QObject::eventFilter(obj, ev);
                                          }
                                          return rc;
                                      }
                                      

                                      Let me know if there's something more you want to look at...thanks.

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 8 May 2020, 18:30 last edited by
                                        #22

                                        A minimal widget that you install that filter on would be nice :-)

                                        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
                                        • M Offline
                                          M Offline
                                          mzimmers
                                          wrote on 8 May 2020, 19:03 last edited by
                                          #23

                                          widget.h

                                          #ifndef WIDGET_H
                                          #define WIDGET_H
                                          
                                          #include <QWidget>
                                          #include "keypress.h"
                                          
                                          namespace Ui {
                                              class Widget;
                                          }
                                          
                                          class Widget : public QWidget
                                          {
                                              Q_OBJECT
                                          
                                          private:
                                              Ui::Widget *ui;
                                              KeyPress *m_keyPress = nullptr;
                                              bool C_pressed = false;
                                              bool D_pressed = false;
                                          
                                          public:
                                              explicit Widget(QWidget *parent = nullptr);
                                              ~Widget();
                                          private slots:
                                              void on_pushButtonQuit_clicked();
                                              void on_pushButtonNics_clicked();
                                              void handleKeyEvent(QEvent event, int key);
                                          signals:
                                              void quitButtonPushed(int rc);
                                          };
                                          #endif // WIDGET_H
                                          
                                          

                                          widget.cpp

                                          #include <iostream>
                                          #include <sstream>
                                          #include <stdio.h>
                                          
                                          #include <QDebug>
                                          
                                          #include "editdialog.h"
                                          #include "nics.h"
                                          #include "widget.h"
                                          #include "ui_widget.h"
                                          
                                          using namespace std;
                                          
                                          Widget::Widget(QWidget *parent) :
                                              QWidget(parent),
                                              ui(new Ui::Widget)
                                          {
                                              ui->setupUi(this);
                                          
                                              // create and connect a KeyPress object.
                                              m_keyPress = new KeyPress(this);
                                              connect(m_keyPress, &KeyPress::keyEvent, this, &Widget::handleKeyEvent);
                                              installEventFilter(m_keyPress);
                                          
                                              // hide the button for the NICs window.
                                              ui->pushButtonNics->setVisible(false);
                                          }
                                          
                                          Widget::~Widget()
                                          {
                                              delete ui;
                                          }
                                          
                                          
                                          void Widget::on_pushButtonQuit_clicked()
                                          {
                                              emit quitButtonPushed(0);
                                          }
                                          
                                          void Widget::handleKeyEvent(QEvent event, int key)
                                          {
                                              if (event.type() == QEvent::ShortcutOverride)
                                              {
                                                  if (key == 'c' || key == 'C')
                                                  {
                                                      C_pressed = true;
                                                  }
                                                  else if ((key == 'd' || key == 'D'))
                                                  {
                                                      D_pressed = true;
                                                  }
                                          
                                              }
                                              else if (event.type() == QEvent::KeyRelease)
                                              {
                                                  if (key == 'c' || key == 'C')
                                                  {
                                                      C_pressed = false;
                                                  }
                                                  else if ((key == 'd' || key == 'D'))
                                                  {
                                                      D_pressed = false;
                                                  }
                                              }
                                          
                                              if (C_pressed && D_pressed)
                                              {
                                                  ui->pushButtonNics->setVisible(true);
                                              }
                                              else
                                              {
                                                  ui->pushButtonNics->setVisible(false);
                                              }
                                          }
                                          
                                          
                                          
                                          void Widget::on_pushButtonNics_clicked()
                                          {
                                              Nics nics(this);
                                              nics.exec();
                                          }
                                          

                                          You'll want to connect the Widget signal to something. Sorry...this was as compact as I dared make it.

                                          Thanks for looking at this.

                                          1 Reply Last reply
                                          0

                                          13/40

                                          7 May 2020, 21:42

                                          27 unread
                                          • Login

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