How to intercept keyboard in QT?

  • 0
    Let's imagine that I have a picture, I want to move it by pressing a key on the keyboard. Perhaps I need to use slots, but how to let the computer know that this or that key is pressed: D
    C++ Juliette Santos, Jul 11, 2019

  • 3 Answers
  • 0

  • 0
    All graphical Qt classes in the protected area have different -event methods.

    by default event is forwarded to the child class. You need to override the appropriate method. For everything else here: doc.qt.io/qt-5/eventsandfilters .html
    Aurora Crawford

  • 0
    We add the keyPressEvent method to our class, and we ourselves call the corresponding methods / slots that will move the image:
    void MainWindow::keyPressEvent(QKeyEvent *e)
    {
    switch (e->key()) {
    case Qt::Key_Up:
    _image->up();
    break;
    case Qt::Key_Down:
    _image->down();
    break;
    case Qt::Key_Left:
    _image->left();
    break;
    case Qt::Key_Right:
    _image->right();
    break;
    }
    }
    Dominick Moreno

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!