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. keyPressEvent
Qt 6.11 is out! See what's new in the release blog

keyPressEvent

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.7k Views 1 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.
  • privatepepperP Offline
    privatepepperP Offline
    privatepepper
    wrote on last edited by
    #1

    Hello,

    Could someone tell me why my keyPressEvent doesn't work when I use arrow keys?
    When I use Qt::Key_W/S/D/A everything works fine. I also tried to use Key_Up/Down..., this way also didn't work

    void MainWindow::keyPressEvent(QKeyEvent *event)
    {
        switch (event->key()) {
            case Qt::UpArrow:
            if (current_square_y != 0 && is_legal_move(current_square_y, current_square_x, current_square_y -1, current_square_x)){
    
                current_square_y--;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
    
            }break;
            case Qt::DownArrow:
            if (current_square_y != maze_height - 1 && is_legal_move(current_square_y, current_square_x, current_square_y + 1, current_square_x)){
    
                current_square_y++;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
    
            }break;
            case Qt::RightArrow:
            if (current_square_x != maze_width - 1 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x + 1)){
    
                current_square_x++;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
    
            }break;
    
            case Qt::Key_Left:
            if (current_square_x != 0 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x - 1)){
    
                current_square_x--;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
            }break;
        }
        if (won_game()){
            QMessageBox::about(this, "t", "You won!!!");
            on_pushButton_3_clicked();
        }
    }
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Qt::UpArrow/Qt::DownArrow/Qt::RightArrow are not Keys.
      I think it should be Key_Up/Down, but you said you already tried that.

      Step 1: Print log in the keyPressEvent no matter what key it is.
      Then try to press up/down to see is there any log from keyPressEvent.
      If there is, then it is simple, you can print the key value in the log.

      Step 2: If there is no log, then maybe the up/down events are handled by other widgets.
      If the MainWindow is the only widget that need to response to key events, you could use

      void QWidget::grabKeyboard()
      

      to make sure all key events go through it.

      1 Reply Last reply
      3
      • privatepepperP Offline
        privatepepperP Offline
        privatepepper
        wrote on last edited by privatepepper
        #3

        How can I print to log to check which key is pressed? I know that I can use qDebug to print to console, but how to print a key which is pressed?

        1 Reply Last reply
        0
        • privatepepperP Offline
          privatepepperP Offline
          privatepepper
          wrote on last edited by
          #4

          oh I figured out how to print to log to check which key is pressed:D
          But it didn't work:/, only with normal keys.

          if (event->key () == Qt::Key_Up){
                  qDebug() << "working";
              }
          
          1 Reply Last reply
          0
          • privatepepperP Offline
            privatepepperP Offline
            privatepepper
            wrote on last edited by privatepepper
            #5

            Okay, so I figured out how to make it to work, I changed my widget focus to StrongFocus, but
            I don't understand why this "setFocusPolicy" makes to work my Key_Up/Down/Left/Right, maybe someone could explain it?

            this->setFocusPolicy(Qt::StrongFocus);
            
            1 Reply Last reply
            0
            • KH-219DesignK Offline
              KH-219DesignK Offline
              KH-219Design
              wrote on last edited by
              #6

              @privatepepper In this code that you shared:

              this->setFocusPolicy(Qt::StrongFocus);
              

              What is the type of "this"? You mentioned it was a widget. Is it a QWidget? If so, then this should be the relevant doc:

              • https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop

              That doc mentions that the default is Qt::NoFocus, and that "You must enable keyboard focus for a widget if it processes keyboard events."

              Does that help?

              I ran into similar confusion, but in QML. I just reread my notes about QML focusPolicy, but the QML related issues seem unlreated to yours. I was hoping maybe some of my "struggle" could translate back to the non-QML world. But I guess you solved your problem anyway, so congrats!

              www.219design.com
              Software | Electrical | Mechanical | Product Design

              privatepepperP 1 Reply Last reply
              0
              • KH-219DesignK KH-219Design

                @privatepepper In this code that you shared:

                this->setFocusPolicy(Qt::StrongFocus);
                

                What is the type of "this"? You mentioned it was a widget. Is it a QWidget? If so, then this should be the relevant doc:

                • https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop

                That doc mentions that the default is Qt::NoFocus, and that "You must enable keyboard focus for a widget if it processes keyboard events."

                Does that help?

                I ran into similar confusion, but in QML. I just reread my notes about QML focusPolicy, but the QML related issues seem unlreated to yours. I was hoping maybe some of my "struggle" could translate back to the non-QML world. But I guess you solved your problem anyway, so congrats!

                privatepepperP Offline
                privatepepperP Offline
                privatepepper
                wrote on last edited by
                #7

                @KH-219Design

                Thanks for the reply :) , but "this" is QMainWindow, oh and good luck with your problem:)

                1 Reply Last reply
                0
                • KH-219DesignK Offline
                  KH-219DesignK Offline
                  KH-219Design
                  wrote on last edited by
                  #8

                  @privatepepper ah. Well based on the doc for QMainWindow:

                  // QMainWindow Class
                  
                  // The QMainWindow class provides a main application window. More...
                  
                  // Header:	#include <QMainWindow>
                  
                  // Inherits:	QWidget
                  

                  Via inheritance, it still "is a" QWidget. So I still think the relevant doc to explain your outcome is:

                  https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop

                  www.219design.com
                  Software | Electrical | Mechanical | Product Design

                  1 Reply Last reply
                  0
                  • privatepepperP Offline
                    privatepepperP Offline
                    privatepepper
                    wrote on last edited by privatepepper
                    #9

                    @KH-219Design

                    Oh:D, thanks for the reply again:), I am such a newbie

                    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