How to detect the modifier key on mouse click in Qt

If you are want to know the modifiers key state from a mouse click event, you can use QGuiApplication::keyboardModifiers() which will retrieve the key state during the last mouse event:

if(QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
    // Do a few things
}

Otherwise, if you want to query explicitly the modifiers state you should use QGuiApplication::queryKeyboardModifiers(). This might be required in other use case like detecting a modifier key during the application startup.


Try QApplication::keyboardModifiers() which is always available

On Qt 5, try QGuiApplication::keyboardModifiers().

Tags:

Qt