How to get 'Shift+1' string sequence from 'Shift+!' QKeyEvent?

  • Hi,

    When I press 'Shift+1' then from QKeyEvent I get key '!' + Qt::ShiftModifier.
    How can I get original 'Shift+1' key sequence string here?
    Or from different side: how to create valid QShortcut from 'Shift+!' string sequence which will handle 'Shift+1' key press?

    I tried to create QKeySequence and use toString() method, but I got 'Shift+!' instead of 'Shift+1'.

    Best regards,

  • Lifetime Qt Champion


    IIRC, you can't shift plus a letter/number will give you the upper cased letter, the character over the number etc... To have a ‘Shift+!’ shortcut you would need a sequence like shift, shift, ! which is not possible

    Have a look at the "QKeySequence": layout issues doc

    Hope it helps

  • Well, I don't want to have 'Shift+!' shortcut. However, I want to create 'shortcut editor' inside my app. Imagine that user wants to set 'Shift+1' shortcut, so he presses 'Shift+1' and then I need to a) show 'Shift+1' in my app (but I get 'Shift+!'), and b) create valid shortcut using string sequence ('Shift+1' shortcut of course).
    I found one solution, but it's ugly: first I create QShortcut for every problematic shortcut sequence, and then I use them instead of QKeyEvent. But this way I cannot be sure that I handle every weird key sequence :(

  • Lifetime Qt Champion

    Add to that that other countries have other keyboard layouts and that would be event more complicated ;)

    Have a look at QtCreator's shortcut editor for inspiration. With my keyboard Shift+1 = + (not Shift +, just +)

    If you don't want to end in world of pain, follow the standard: shortcuts are being made with CTRL, META, ALT with SHIFT used in conjunction with one or more of the first three keys.

    Hope it will ease your work

  • First of all thank you for your time and clues.

    I need to clarify that by Shift+1 I meant 'press Shift along with 1', not 'press Shift along with + and with 1'. Sorry for misguiding you.

    Unfortunately CTRL, META, ALT with SHIFT seem not to be valid solution too. Try 'CTRL SHIFT 1' in QtCreator, you will get unusable shortcut 'CTRL SHIFT !' :(

  • Lifetime Qt Champion

    You're welcome !

    I understood you right and that was exactly my point: there are some combinations that are simply invalid or not feasible and these can change between keyboard layouts (trust me, there are many different).

    Don't try to trick the system to make them work, you would end up having to maintain a very complex beast. Also think of the consequences of having a shortcut of shift+1 while editing a text ?

    Hope it helps

  • You're right about editing text and Shift+1, there is clear conflict. So from the other side: do you see any possibility to recognize/block such shortcuts? I wouldn't like to give possibility to define something which cannot be used. Any ideas?

Log in to reply

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