Search code examples
qtkeypress

Qt get application state without event listener


I need to know if a key (let's say r) is pressed when the main() start. See :

int main(int argc, char *argv[])
{
    if(R is pressed)
    {} // Do a few things

    // Do amazing stuff whatever happened
    return a.exec();
}

But I can't find a way to do it for all platforms (win, mac, lin), the only thing I found is a trick for windows : Using GetKeyState() which is not very satisfying...


Solution

  • If you want to check modifier keys (shift, control, alt) you can use QGuiApplication::queryKeyboardModifiers() :

    int main(int argc, char *argv[])
    {
        if(QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier))
        {} // Do a few things
    
        // Do amazing stuff whatever happened
        return a.exec();
    }