qtkeyboard-shortcuts

How to verify whether a shortcut-keysequence is valid?


In my application I allow the user to enter shortcuts in a line-edit. E.g., if the user enters Ctrl + F10, this means that a certain action will be executed if the user presses F10.

I use the QKeySequence class to convert the string Ctrl + F10 to a QKeySequence, and then pass this QKeySequence to the QShortCut's constructor.

Is there a way to verify that the QKeySequence is valid for a shortcut? E.g. if the user enters Ctrl + Shift, this is converted to a QKeySequence, but I don't want to allow this for a shortcut. Same with strings like Ctrl + F10 + Ctrl (which becomes Ctrl), ...


Solution

  • You could prevent the user from entering himself the shortcut text, and capture the key directly (you would still have to filter "orphan" modifier keys manually).

    See How can I capture QKeySequence from QKeyEvent depending on current keyboard layout? for a working example.