Keyboard shortcuts on the web
Tags: frontend, a11y, keyboard, tipsandtricks
Published: 2020-11-08 20:11:02

---
title: Keyboard shortcuts on the web
published: true
tags: frontend, accessibility, keyboard, tipsandtricks
canonical_url: https://golsteyn.com/projects/projects/designing-keyboard-shortcuts/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/nhuq278w0rb11g0txpt1.png
---

_This post is available on my [personal website](https://golsteyn.com/projects/designing-keyboard-shortcuts/) and its content is shared here on dev.to._

---

**Keyboard interfaces are an important part of any user-facing application.** The ability to use the keyboard can help surface frequent or more complex tasks to the user, thereby increasing productivity and accessibility.

At the same time, we have to ensure that our application follow guidelines consistent with other applications and the platform running our interface (also known as the principle of least astonishment). Failure to do so would result in applications that require additional learning time and are more error-prone.

Therefore, deciding on which actions to make accessible to the user via keyboard shortcuts and the shortcuts to use are important UX considerations.

<b><i>Note:</i></b> keyboard navigation, although relying on the keyboard inputs, has different implications than keyboard shortcuts. See the [ARIA authoring practices](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav) for more information on keyboard navigation.

## Considerations when designing a keyboard interface

When determining which shortcuts to add to an application, we can answer two high-level questions:

1. **Which actions should be accessible via a keyboard shortcut?** Requires an understanding of the actions the user may take in a particular interface.
2. **Which keyboard shortcut should I use for this particular action?** Requires an understanding of guidelines surrounding the design of keyboard interfaces.

When deciding on which actions to assign keyboard shortcuts, we should include common actions (such as copy/cut/paste, open, save, close, etc.) and application-specific actions the user will need to access regularly.

### Choosing keyboard shortcuts

When selecting which key combination to use for each action, we can answer the [following three questions](https://ux.stackexchange.com/a/105659): 

1. **What is the standard key combination for a particular action?** For example, saving is commonly accessible via <kbd>Ctrl+S</kbd>.
2. **What is the key combination found in similar applications?** In a text editor, <kbd>Ctrl+B</kbd> bolds text. Other text editors should reuse this pattern.
3. **What is the key combination found in applications used alongside ours?** For example, consider how GitHub or Bitbucket share similar keyboard shortcuts to code editors such as Atom or VS Code.

If designing a keyboard shortcut for functionality not found in existing applications, [we should strive for shortcuts that are easy to remember and do not conflict with existing browser or operating system keyboard shortcuts](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_shortcuts_assigning).

Mnemonics (such as using <kbd>Ctrl+U</kbd> for <u>U</u>nderline) can help make shortcuts easy to remember.

It is also important to consider localization when designing keyboard shortcuts, as different languages and keyboard layouts may make certain key combination less preferable (check out this [list of keyboard layouts](http://kbd-intl.narod.ru/english/layouts) to determine which layouts are applicable to your application).

### Modifier keys

Most keyboard shortcuts use one or more modifier keys. Used effectively, they can help organize keyboard shortcuts and allow for multiple actions to be linked to one key (consider the relationship between Undo and Redo, using <kbd>Ctrl+Z</kbd> and <kbd>Ctrl+Shift+Z</kbd> respectively).

The modifier key to use for a particular action will depend on the platform: 

- **[Windows/Linux](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/dnacc/guidelines-for-keyboard-user-interface-design) —** <kbd>Ctrl</kbd> for large-scale effects, <kbd>Shift</kbd> for actions that complement the existing standard shortcut. <kbd>Alt</kbd> and <kbd>⊞ Win</kbd> should be avoided as they are used extensively for operating system functions.
- **[Apple](https://developer.apple.com/design/human-interface-guidelines/macos/user-interaction/keyboard/) —** <kbd>⌘ Command</kbd> should be used preferably, <kbd>Shift</kbd> for actions that complement the existing standard shortcut. <kbd>Option</kbd> should only be used when the <kbd>⌘ Command</kbd> modifier is already taken. <kbd>Ctrl</kbd> should be avoided as it is used extensively for operating system functions.

**Modifier keys are not always necessary.** Interfaces that do not have a strong text entry focus can avoid modifier keys altogether (for example, GitHub or Photoshop). This can help speed up access to certain actions.

## Scope of keyboard shortcuts

### The distinction between application, browser and OS shortcuts

Web applications have to take into consideration three levels of scope inherent to their execution environment: the operating system, the browser, and the application itself.

<figure>
![For pointer inputs, the visual hierarchy can help users determine which part of the screen belongs to the operating system, browser, or application scope.
](https://dev-to-uploads.s3.amazonaws.com/i/6cagtcso8xniejryyrne.png)
<figcaption>For pointer inputs, the visual hierarchy can help users determine which part of the screen belongs to the operating system, browser, or application scope.
 </figcaption>
</figure>

In contrast to pointer-based inputs (mouse or touch gestures), it is difficult for the user to determine at which scope a particular keyboard shortcut will act on.

For pointer-based inputs, the visual hierarchy of UI elements on the screen will help the user identify what belongs to the operating system, the browser, or the application. This distinction is less clear for keyboard inputs. While the use of modifier keys can help label certain keyboard shortcuts as belonging to a certain scope (using the <kbd>⊞ Win</kbd> modifiers for all OS-level shortcuts on Windows for example), these are not always obvious to the user and may be difficult to discover.

As an example, consider the difference between <kbd>⌘+←/→</kbd> and <kbd>Ctrl+←/→</kbd> on Mac. The first is a browser-level shortcut, allowing the user to navigate between tabs. The second is an OS-level shortcut allowing the user to navigate desktop views.

### Avoiding keyboard shortcut conflicts

As an application author, we should avoid keyboard shortcuts that override browser or OS defaults or are similar to existing system shortcuts.

Of course, as with everything when design web applications, we have to take into consideration the different platforms our application will run on. Keyboard shortcuts vary between operating systems and browsers and our applications should handle all these cases.

### Keyboard shortcut scoping from within a web application

As a web application changes, the part of the user interface receiving keyboard input may change as a result. Dialogs, tree views, dropdown views may take priority in receiving keyboard inputs over the main application. The part of our interface that receives keyboard actions (eg. focus) needs to be clear to the user through some sort of visual indication.

Besides, there are no native approach on the web to restrict keyboard inputs to only a part of our web application (in the case of dialogs for example). We are responsible for implementing this logic.

[It is important to ensure that keyboard shortcuts do not take different meaning in differerent parts of our application](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/dnacc/guidelines-for-keyboard-user-interface-design) (for example <kbd>Ctrl+D</kbd> taking a different meaning across screens). 

## Discoverability of keyboard shortcuts

Applications usually provide a list of all keyboard shortcuts in a dedicated screen. However, other approaches exist to help introduce users to shortcuts as they interact with the application.

<figure>![Example of a keyboard shortcut help screen on YouTube. It can be accessed by pressing ?.](https://dev-to-uploads.s3.amazonaws.com/i/xdx2uemk8smlhcu22p1m.png)<figcaption>Example of a keyboard shortcut help screen on YouTube. It can be accessed by pressing <kbd>?</kbd>.</figcaption></figure>
<figure>![All macOS menus list keyboard shortcuts alongside each menu item.](https://dev-to-uploads.s3.amazonaws.com/i/x5ub0fl6pioxy0khrerh.png)<figcaption>All macOS menus list keyboard shortcuts alongside each menu item.</figcaption></figure>
<figure>![Google Photos suggests a keyboard shortcut if the users performs an action repeatedly.](https://dev-to-uploads.s3.amazonaws.com/i/9f6l4mx5qa4bgxdoyiol.png)<figcaption>Google Photos suggests a keyboard shortcut if the users performs an action repeatedly.</figcaption></figure>

In general, when identifying an approach to let the user discover keyboard shortcuts, [we can consider the following guidelines](https://ux.stackexchange.com/a/9340): 

1. Aim for solutions that allow for discovery through mouse-based inputs
2. Look for incremental discovery
3. Provide an easily accessible cheat sheet of important keyboard shortcuts

## Chords, modes, and palettes

Keyboard chords and modes are core to Emacs and Vim. They are both examples of approaches to make a large number of actions accessible from the keyboard.

Chords (which consist of two keypresses to achieve a keyboard action) can be a powerful way to create more keyboard shortcuts using a common keyboard shortcut to start a chord sequence (<kbd>Ctrl+C</kbd> and <kbd>Ctrl+X</kbd> in Emacs).

Modes are a Vim feature that assigns different keyboard shortcuts depending on which mode is currently active.

Both these approaches are non-intuitive and require a significant amount of training from the user to learn the sequences for each keyboard action. Once the user becomes familiar with these interfaces, they can be a very effective way to expose a considerable number of actions with the keyboard. Chords and modes may only make sense in specialized applications where a large number of actions are available to the user at any point in time.

<figure>![Command palette in VS Code. Notice that the palette also suggests keyboard shortcuts to the user.](https://dev-to-uploads.s3.amazonaws.com/i/7f54l49rxinn7qzg9mix.png)<figcaption>Command palette in VS Code. Notice that the palette also suggests keyboard shortcuts to the user.</figcaption></figure>

Palettes can provide an alternative that strikes a balance between ease of use and completeness. Palettes allow the user to search and select actions through a search bar which is usually invoked via a keyboard shortcut. Both Vim and Emacs provide this capability, but they have appeared in other applications such as VS Code or Figma for example.

## Customizability

In certain scenarios, allowing users to customize available keyboard shortcuts may make sense. Video games often allow users to modify keyboard mapping, to cater to the user's preferred gaming style, or support certain gaming hardware.

For complex applications, customizability can allow the user to adapt the interface to their preference, making the interface easier to use. The ability to customize keyboard shortcuts adds complexity, however, and most applications should not provide this feature.

## Resources

- [Apple Human Interface Guidelines - Keyboard](https://developer.apple.com/design/human-interface-guidelines/macos/user-interaction/keyboard/)
- [Microsoft Guidelines for keyboard user interface design](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/dnacc/guidelines-for-keyboard-user-interface-design)
- [GNOME Keyboard input guidelines](https://developer.gnome.org/hig/stable/keyboard-input.html.en)
- [ARIA Authoring Practices](https://www.w3.org/TR/wai-aria-practices-1.1)
- [Wikipedia Table of keyboard shortcuts](https://en.wikipedia.org/wiki/Table_of_keyboard_shortcuts)