What is difference between hotkey, shortcut and accelerator key?

前端 未结 3 1343
灰色年华
灰色年华 2021-01-31 19:00
  1. What is the difference about them?

  2. In Qt, if I have a hotkey for QPushButton, I can make it by \"Alt + ?\", but if it\'s for qaction, I can press \"?\"

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 19:22

    Alf's answer is correct for Windows applicability. Your terms you mention (hotkey/shortcut/accelerator) don't sound familiar from a pure Qt point of view.

    In Qt you can elect to handle key sequences yourself or you can use Qt's own simplification method. Either way you must remember that Qt itself targets many platforms on which a key combination may or may not make sense. The classic Alt + F4 makes sense on a keyboard, but on a mobile device you don't have an Alt modifier or an F4 key. What you really want is a way of specifying a generic close the application shortcut. This problem is multiplied because the symbol may be available but the key sequence to reach it might be different on other keyboard layouts. This section of the documentation provides a good example.

    Qt handles this with class QKeySequence. The very clever Qt developers have provided an easy way of defining common user actions and those actions will use key combinations that are default to the target platform. It does this using enum QKeySequence::StandardKey and in the close the application example, you could use this like so:

    QAction exitAction;
    exitAction.setShortcut(QKeySequence(QKeySequence::Quit));
    

    This is all explained in the documentation. There are a two other modifiers (shortcutContext() and softKeyRole()) which can be applied to QActions which effect their application in more advanced ways.

    You are also free to assign your own shortcuts using something like:

    QAction helpAction(tr("&?"));
    helpAction.setShortcut(QKeySequence(tr("ALT+?")));
    

    The first line applies the (translated) text "?" to the action which will appear as its text on a menu or button. Note that the question mark symbol might not be the right symbol in all languages so the translate method allows a translator to assign a more appropriate symbol if required. The ampersand symbol means the character immediately after will be the short-cut key when the menu is active.

    The second line assigns the (translated) shortcut of Alt + ? and in this example the Shift modifier will be handled by the platform if required. Again, the tr() method allows the translator to specify a more appropriate shortcut if available.


    In response to teukkam's comment:

    If you mean you simply want your button to be triggerable by a keystroke whether its modified by Alt or not then you could do something like:

    QPushButton* yourButton; // assign this pointer yourself
    yourButton->setText(tr("&Process"));
    yourButton->setShortcut(tr("p"));
    

    In this example, the ampersand in setText() does the same as the previous example, and the translate function is used in the same way.

    The setShortcut() method just uses the letter "p" so should work now with or without the Alt modifier. A quick skim of the documentation suggests this will work with or without the Shift modifier as the letters in a key sequence are apparently case-insensitive.

    Also, P would be a bad choice as its often assumed to be the print command.

    A final note if you're defining hard coded short cuts, make sure they work on all your target platforms!

提交回复
热议问题