Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cross-platform shortcut key sequences



  • This is a question about the shortcut key sequences you can assign to a QAction, which is going to be used on the main window menu.

    I have been given an application which has been designed on Mac from Qt Creator. The target platforms will additionally be Linux & Windows.

    The programmer has included shortcuts for items, all of which are Meta+... The resulting code (PySide2, but that's not the point) includes for example:

    self.action_paste.setShortcut(QtWidgets.QApplication.translate("MainWindow", "Meta+V", None, -1))
    self.action_redo.setShortcut(QtWidgets.QApplication.translate("MainWindow", "Meta+Shift+Z", None, -1))
    self.action_zoom_reset.setShortcut(QtWidgets.QApplication.translate("MainWindow", "Meta+0", None, -1))
    self.action_stop.setShortcut(QtWidgets.QApplication.translate("MainWindow", "Meta+Alt+S", None, -1))
    

    so you can see some are plain Meta, some have additional modifier keys. They were constructed in Qt Creator by pressing the keys, and when you pull down the menu the user is shown the text Meta+... on the right side of each item.

    Now, I have several questions/problems here.

    1. Being a Real Programmer, I have never used a Mac(!) Is using Meta what Mac users will expect? What about Ctrl or Alt or that Mac Command key?

    2. It took me an hour to discover that on my Linux virtual box running under Windows, I need to press the actual Windows keyboard key for Meta. No Windows/Linux user will know this, it wouldn't be called "Meta", and I believe the Windows key should only be used by Windows anyway. Or, I can't use e.g. Windows+0 anyway because Visual Studio code grabs it away from my app!

    3. If we're going to use, say, Meta+ under Mac and Ctrl+ or Alt+ under Windows/Linux, how are we supposed to design it from Qt Creator and how are we supposed to translate the text on the menu which tells user what to press?

    4. On top of all this, for all I know the key sequences expected by users will actually differ across platforms. Maybe for Windows Ctrl+C for copy Mac users will actually expect Meta+C or even Alt+/ or something. Or, for say zooming I think I've seen quite different sequences used in a PC app from a Mac one, where they are not even "standard" key sequences like Qt has the enum Qt::key Qt::Key_Copy for (which I don't see how to specify from Qt Creator anyway...). Sigh :( [I accept this point probably has no good answer, please don't let that stop you from answering the previous points though.]

    So.... in a word, how do you go about designing shortcuts in an easy to use way at design-time while maintaining user expectations at runtime when targetting cross-platform like this?


  • Lifetime Qt Champion

    Hi,

    The Mac Command key matches the Ctrl key on Windows (unless you are in a terminal, there the usual keys apply).

    You should take a look at QKeySequence.

    I think you have the answers to all your questions there :-)

    The short version is: use standard sequence whenever possible. Then use Ctrl for cross platform (it will translate to Cmd on Mac) and for custom stuff, be explicit in the code rather than using numerical combination.



  • @SGaist
    Believe me, I have looked at QKeySequence. So:

    1. And how do you "use standard sequence whenever possible" from Qt Creator, where it just allows you to physically press the shortcut keys? It doesn't offer me a symbolic constant like Qt::Key_Copy.

    2. So my Mac user is "unusual" in having picked Meta+ for all the shortcuts? You would have expected him to have picked Command+?

    3. If the Mac Ctrl key is labelled and known as Cmd, how will the explanatory text on the menu telling the user what the shortcut is, which I can see in the .ui file, read appropriately/differently across platforms?

    If you can answer those 3 I'd be so obliged :)


  • Lifetime Qt Champion

    1. You was thinking about QKeySequence::StandardKey.
    2. Yes for unusual and no, use Ctrl.
    3. The Mac keyboard has a Ctrl key too however, the usual shortcuts you are used to on Linux and Windows using the Ctrl key will use the Cmd key on Mac. But in your code, there's nothing special for you to do, just use Ctrl in your code. It will be adapted on Mac to use Cmd.


  • @SGaist
    Yes, I was thinking of QKeySequence::StandardKey. I will read whole of QKeySequence tomorrow.
    I will discuss this with the Mac user.
    Thanks.


  • Lifetime Qt Champion

    Don't fall into the trap ! First read then discuss ;-)



  • @SGaist
    Finding the ui file containing Meta+ and then having that show up under Linux where I couldn't figure what to actually press has made me throw a wobbly!
    I am pretty good at reading doc pages, I just thought we were talking about trying to use the StandardKeys.
    :)



  • @SGaist
    Right, I have now carefully read the whole of https://doc.qt.io/qt-5/qkeysequence.html as you advised. Yes, it explained a bit about Mac differences, I have yet to test that out with my Mac user.

    However, one of my questions remains unanswered. You will note the doc page admonishes:

    It is preferable to use standard shortcuts where possible.

    i.e. the enum QKeySequence::StandardKey you picked up I was thinking about:

    This enum represent standard key bindings. They can be used to assign platform dependent keyboard shortcuts to a QAction.

    I wholeheartedly agree this is desirable in a cross-platform situation. However, as I said earlier, how you are supposed to use this when designing from Qt Designer? No matter where I try to set the shortcut for an action in Designer, anything I type is taken as literal key presses and encoded as such into the .ui file. I do not have an "alternative" interface which I can pick/type in enum QKeySequence::StandardKey (I thought it might be pickable from a dropdown combo, but no).

    Sooo... is it correct that the docs could state "While using QKeySequence::StandardKey from code is the best way to do stuff in code, note that this approach cannot be used if the UI is designed via Qt Designer"?

    EDIT: Ah ha, I finally came across https://forum.qt.io/topic/6259/qt-designer-does-not-support-qkeysequence-standardkey in this forum. So exactly this issue was raised as a"bug" with qt.nokia.com in 2011, but left as not doable from Designer for the past 9 years?


Log in to reply