Adding custom hot keys

Adding custom hot keys, or keyboard shortcuts, increases productivity by helping users to perform common tasks quickly and efficiently. You can also create custom hot keys to focus on important fields or selections and reduce the number of clicks that a user needs to take to perform actions.

About this task

Note: You can add custom hot keys for any Order Hub screen except the ones that are within buc-app-sfo.
You add custom keyboard shortcuts, or hot keys to your application by using the keybinding system. The keybinding system consists of two main components:
  • Hot key definitions, which define the keyboard shortcuts and their behavior.
  • Translation keys, which provide descriptions for the shortcuts.

Procedure

  1. For the page to which you want to add the custom hot keys, create or locate the buc-page-definitions.json file.

    Update or create the JSON file within the module's src-custom/assets/custom folder. For example, packages/<route>/src-custom/assets/custom/buc-page-definitions.json.

    If no additional customizations are required other than the changes in the JSON file, the JSON file does not need to be associated to any route. Instead, create the JSON file under packages/<app>-root-config/src/assets/custom/buc-page-definitions.json. Doing so can ensure that future Order Hub releases are automatically applied without manually synchronizing your customization code.

    To find the pages that support the addition of hot keys, see the pages that are listed in the following file.
    • buc-app-<module>/packages/<module>-shared/assets/buc-app-<module>/buc-page-definitions.json
  2. Define hot keys within the hotkeys object of your page in the buc-page-definitions.json file. Include the following properties.
    Key id
    A unique ID for the hot key. For example, "go-next" or "initiate-save."
    Values(Object) id
    A unique ID. You can reuse the object ID.
    description
    A reference to a translation string that describes the hot key action. For example, "CREATE_ORDER.KEY_BINDINGS.GO_NEXT."
    type
    A string that references the type of action to perform. For example, "click" or "focus." Click simulates clicking the target element and is used for buttons and links. Focus moves the keyboard focus to the target element and is used for input fields.
    elementIdentifier
    A string that represents a CSS query selector to target the element. For example, "[tid='button-id'] button", "input#field\ Name", or "buc-label#contract-field input." Use test IDs, or tid attributes when available for stability. Be specific to avoid targeting multiple elements. Test CSS selectors to help ensure that they target the correct element, and consider dynamic content and make sure that selectors work across different states.
    keybinding
    A string of keys, or keyboard combination to initiate the action delimited by +. For example, "shift+n", "ctrl+s", or "shift+1." Supported modifiers are shift, ctr, alt, and meta. You can use a single key, or include one or more modifiers. Use shift combinations for common actions. When you determine the strings, make sure to avoid conflicts with browser shortcuts and be consistent across similar features.
    keybinding_$(locale)
    An optional string of keys to use when a user is using a specific locale. If this property is not set, the default property is keybinding.
    See the following example.
    {
      "create-contract-order": {
        "name": "create-contract-order",
        "actions": [],
        "tabs": [],
        "hotkeys": {
          "go-next": {
            "id": "go-next",
            "description": "KEY_BINDINGS.GO_NEXT",
            "keybinding": "shift+n",
            "type": "click",
            "elementIdentifier": "[tid='contract-order-create-next'] button"
          },
          "go-previous": {
            "id": "go-previous",
            "description": "KEY_BINDINGS.GO_PREVIOUS",
            "keybinding": "shift+p",
            "type": "click",
            "elementIdentifier": "[tid='contract-order-create-previous'] button"
          },
          "initiate-save": {
            "id": "initiate-save",
            "description": "KEY_BINDINGS.INITIATE_SAVE",
            "keybinding": "shift+s",
            "type": "click",
            "elementIdentifier": "[tid='contract-order-create-save'] button"
          },
          "initiate-confirm": {
            "id": "confirm-contract-order",
            "description": "KEY_BINDINGS.INITIATE_CONFIRM",
            "keybinding": "shift+c",
            "type": "click",
            "elementIdentifier": "[tid='contract-order-create-confirm'] button"
          },
          "focus-first-element": {
            "id": "focus-first-element",
            "description": "KEY_BINDINGS.FOCUS_FIRST_ELEMENT",
            "keybinding": "shift+1",
            "type": "focus",
            "elementIdentifier": "buc-label#contractOrderName input"
          }
        }
      }
    }
  3. Add translation keys for the custom hot key within KEY_BINDINGS in the appropriate en.json file.
    Some guidelines for translation.
    • Be descriptive. Clearly explain what the keyboard shortcut does.
    • Use action verbs. Start with verbs such as navigate, save, move, or activate.
    • Be concise. Keep descriptions short but informative.
    • End with a period.
    {
      "CREATE_CONTRACT_ORDER": {
        "COMMON": {
          "ROUTE_CREATE_CONTRACT_ORDER": "Create contract order",
          ...
        },
        "KEY_BINDINGS": {
          "GO_PREVIOUS": "Navigate to the previous page.",
          "GO_NEXT": "Navigate to the next page.",
          "INITIATE_SAVE": "Save changes.",
          "INITIATE_CONFIRM": "Activate/Confirm.",
          "FOCUS_FIRST_ELEMENT": "Move to the name field."
        }
      }
    }

What to do next

After you create your hot keys, you will want to test them to make sure that they work as expected. Test your hot keys by using the following process.
  1. Build the application to help ensure that no syntax errors exist.
  2. Go to the page where the hot keys are defined.
  3. Press the keyboard combination to test the functions.
  4. Verify that the action occurs as expected.
Troubleshooting help for when a hot key is not working
  • Verify that the element identifier targets the correct element.
  • Make sure that the element identifier is a valid CSS selector. If the element identifier includes spaces, either include a backslash before the space (for example input#field\ Name), or rewrite the element identifier without spaces.
  • Check that the element is visible and enabled.
  • Make sure that the keybinding system hot key definition does not conflict with any browser shortcut.
  • Confirm that the page name matches in initializeHotKeyListener().
  • If the target element is not normally interactive (for example, a <div>), you must make it focusable by adding a tabIndex attribute before a hot key can trigger it.
Troubleshooting help for when the translation is not showing
  • Verify that the translation key path matches exactly.
  • Check that KEY_BINDINGS is in the correct parent object.
  • Make sure that the en.json file is a valid JSON file.
Troubleshooting help for when the element is not found
  • Use browser Dev Tools to verify the CSS selector.
  • Check to make sure that the element is rendered conditionally.
  • Make sure that the selector is specific.