What is difference between hotkey, shortcut and accelerator key?

前端 未结 3 1341
灰色年华
灰色年华 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:17

    In Windows:

    HotKey

    Keyboard key or combination of keys that execute a command in a given context.

    Shortcut

    Multi-key HotKey with no menu navigation restrictions nor gui elements required.

    AccessKey

    Single key HotKey which command is to activate a visible command control (requires gui element) that is captioned/labeled with the corresponding hotkey letter underscored.

    Accelerator Keys

    Multi-key HotKey which command is to activate a command control (requires gui element) regardless of its visibility.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-31 19:23

    In Windows, an accelerator key is application global; e.g. Alt+F4.

    A shortcut key is part of the name of a menu item or button, where it can be underlined, and is available (without modifiers) when that menu item or button is directly available.

    From Microsoft:

    A hot key is a key combination that the user can press to perform an action quickly. For example, a user can create a hot key that activates a given window and brings it to the top of the z-order.

    which seems to indicate that hot keys are system global.

    To sum up:

    • shortcut key = no modifiers, local in menu or (for button) in window
    • accelerator key = typically with modifier, application global
    • hot key = apparently system global

    I don't know about specific meanings in Qt; for that see the Qt documentation.

    0 讨论(0)
提交回复
热议问题