Checking for letters pressed when mouse is pressed

  • Hi everyone !

    I'm developing an application using Qt in C++.

    When there is a mouse click, I would like to check if there are letters pressed on the keyboard. So far, I found solutions when the keys pressed are specific ones like Shift, Control .. but not when a letter is pressed.

    Do you have any advice for me ?

    PS: Sorry if this topic isn't in the right category, I wasn't sure where to post it.

    Thank you !


  • Combine

    virtual void	mousePressEvent(QMouseEvent * event)


    virtual void	keyPressEvent(QKeyEvent * event)

  • Moderators

    Hi @LancelotPops ,

    welcome to the Qt forum.

    Did you have a look at queryKeyboardModifiers ?

  • Indeed, my application is a QSlicerApplication (I'm developing a module in 3DSlicer) and I'm not sure I can use exactly the same solutions as if I were only working with Qt.

    I tried this :

    when leftButtonPressEvent :

    Qt::KeyboardModifiers modifier = qSlicerApplication::application()->keyboardModifiers();
         if (QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) == true) {
            std::cout << "shift pressed" << std::endl;

    This works very well ... but it only works for specific keys (Control, Shift ..), is there something like that but working for letters pressed ?

  • Moderators


    this topic may be of interest to determine the keys.

    Hope it helps,


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you explain why do you need to know whether a letter is currently pressed ?

    A long press on such a key usually triggers the auto-repeat of the system so it might make things a bit more difficult.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.