multiple shortcut key doesn't work

471 views Asked by At

I put on multiple shortcuts on each QAction, e.g. 'L, Right, Space'. but 'L' shortcut doesn't now work. 'Right' and 'Space' can work.

Similarly, if I register 'A, B' and then, 'A' will not work and 'B' will work.

If I use QMainWindow::eventFilter() myself, instead of using QAction::setShortcut(), all shortcut keys will work.

In this case, of course, the shortcut key text is not displayed on the main menu.

I tested on Windows 7 x64, and Qt-5.9.1

QMap<QString, QAction*>& actions = qApp->keyActions().actions();
QMap<QString, QKeySequence> & seqMap = qApp->keyActions().keyMaps();
foreach(const QString& name, actions.keys()) {
    QAction* a = actions[name];
    QKeySequence seq = seqMap[name]; // e.g. QKeySequence("L, Right, Space")
    a->setShortcut(seq);
    a->setShortcutContext(Qt::ApplicationShortcut);
}
1

There are 1 answers

0
Felix On BEST ANSWER

As the name "QKeySequence" suggests, it is a sequence of keys that have to be pressed. The string "L, Right, Space" means that the user will have to press all 3 keys in that order to invoke the action!

If you want multiple "parallel" keys, i.e. the action should react for any of those keys, not the sequence, use QAction::setShortcuts and create one sequence for every key. (you can use QStringList::split to split the string into the 3 keys.)