User Agents

Web browsers and everyday web tech, explained

Chrome Shortcuts in PWA: How to Define

· 6 min read · Browsers

In this article
  1. Background: What Are PWA Shortcuts?
  2. How to Define Shortcuts in the Manifest
  3. Handling Shortcut Activation in Your Code
  4. Choosing Good Shortcut Keys
  5. Common Problems and Misconceptions
  6. Comparison: PWA Shortcuts vs. Extension Commands
  7. Safety and Best Practices
  8. Frequently Asked Questions

To define keyboard shortcuts in a Progressive Web App (PWA) that runs in Chrome, you declare them in the web app manifest using the shortcuts member and handle the activation in your service worker or page script. This lets you assign custom key combinations (e.g., Ctrl+Shift+N) that trigger specific actions, like opening a new note or navigating to a settings page.

Background: What Are PWA Shortcuts?

PWA shortcuts are user-defined keyboard combinations that work when the PWA is installed and focused in its own window (not in a regular browser tab). They are part of the Web App Manifest specification, which is a JSON file that describes how your app behaves when installed. The shortcuts member was added to the manifest spec to let developers offer quick actions from the app's icon (via right-click) and, in some browsers, as keyboard shortcuts.

Chrome supports the shortcuts member for installed PWAs on desktop platforms (Windows, macOS, Linux, and ChromeOS). When you define a shortcut, Chrome displays it in the app's context menu (right-click on the taskbar or dock icon) and allows you to assign a keyboard combination to it. The actual key binding is usually set by the user or by the operating system, but Chrome provides a default suggestion based on the shortcut's name and shortcut fields.

How to Define Shortcuts in the Manifest

To define shortcuts, add a shortcuts array to your manifest.json file. Each shortcut object must contain a name (a string), a url (the address to open when the shortcut is activated), and an optional shortcut field that specifies the suggested key combination. The shortcut field uses the same format as the shortcuts API in Chrome extensions: modifiers like Ctrl, Alt, Shift, and Meta (the Command key on Mac), followed by a key. For example, "Ctrl+Shift+N".

Here is a minimal manifest snippet:

{
  "name": "My PWA",
  "shortcuts": [
    {
      "name": "New Note",
      "url": "/new-note",
      "shortcut": "Ctrl+Shift+N"
    },
    {
      "name": "Settings",
      "url": "/settings",
      "shortcut": "Ctrl+Shift+S"
    }
  ]
}

After updating the manifest, you must re-install the PWA (or refresh it if it's already installed) for Chrome to pick up the changes. The shortcuts appear in the app's context menu, and users can also assign their own key combinations via the operating system's app shortcuts settings (e.g., on Windows, right-click the app icon, go to Properties, and set a shortcut key).

Handling Shortcut Activation in Your Code

When a user activates a shortcut (either via the context menu or a keyboard combo), Chrome navigates to the url specified in the shortcut definition. Your app must then handle that URL and perform the desired action. This is similar to handling a normal navigation: you can use a client-side router (like React Router) or a service worker to intercept the request and return a custom response.

For example, if your shortcut points to /new-note, your service worker can intercept that fetch and return a minimal HTML page that triggers a JavaScript function to create a new note, or you can simply let the page load and have your app's script check the URL on startup. The key is to ensure that the URL is a real route in your app, not just a placeholder.

Choosing Good Shortcut Keys

When choosing shortcut keys, avoid conflicts with browser or OS-level shortcuts. For instance, Ctrl+N is typically reserved for opening a new window, and Ctrl+T opens a new tab. Common safe choices include:

  • Ctrl+Shift+<letter> (e.g., Ctrl+Shift+N, Ctrl+Shift+S)
  • Alt+<letter> (e.g., Alt+N, Alt+S) on Windows/Linux
  • Command+Shift+<letter> on macOS

Also, consider that the shortcut field is only a suggestion; users can change it. So design your app to work even if the shortcut is not set, by also providing a menu or button for the same action.

Common Problems and Misconceptions

One common misconception is that the shortcuts member in the manifest automatically registers a keyboard listener in your page. It does not. The manifest only defines the shortcut's metadata and the URL to navigate to. You still need to handle the navigation in your app's code.

Another issue is that shortcuts only work when the PWA is installed and running in its own window. If the user opens the PWA in a regular browser tab, the shortcuts are not available. Also, Chrome does not support the shortcuts member on mobile devices; it is a desktop-only feature.

If your shortcuts do not appear, check that:

  • Your manifest is correctly linked and valid JSON.
  • You have re-installed the PWA after updating the manifest.
  • The url is relative to the manifest's scope.
  • You are using the correct modifier syntax (e.g., Ctrl not Control).

Comparison: PWA Shortcuts vs. Extension Commands

PWA shortcuts are different from Chrome extension commands. Extensions use the commands API in the background script, which can listen for key events and execute arbitrary code. PWA shortcuts, on the other hand, are declarative and only navigate to a URL. This makes them simpler but less flexible. If you need to perform complex actions (like toggling a setting without navigating), you might need to use a service worker with a message channel or use the Web Share Target API, but that is beyond the scope of PWA shortcuts.

Safety and Best Practices

When defining shortcuts, be careful not to override system-wide shortcuts that could interfere with the user's workflow. Also, ensure that the URLs you use are within your app's scope to avoid security issues. Since shortcuts can be triggered by the user at any time, make sure your app handles the navigation gracefully, especially if the user is in the middle of a task. Use the beforeunload event if you need to warn about unsaved changes, but avoid making it intrusive.

Frequently Asked Questions

Do PWA shortcuts work in Chrome on Android?

No, the shortcuts member is only supported on desktop versions of Chrome. On Android, PWAs can use the Web Share Target API or other features, but not keyboard shortcuts.

Can I define a shortcut that runs JavaScript instead of navigating?

No, the manifest shortcuts member only allows a URL. To run JavaScript, you would need to navigate to a URL that your app interprets as a command, or use a service worker to intercept the request and execute logic.

How do users see the shortcuts?

Right-click the PWA's icon in the taskbar or dock. The shortcuts appear in the context menu with their names and key combinations (if defined). Users can also assign custom key combinations via the operating system's app properties.

What happens if two shortcuts have the same key combination?

Chrome will likely use the first one in the shortcuts array. To avoid confusion, ensure each shortcut has a unique shortcut field.

Can I update shortcuts without reinstalling the PWA?

You need to update the manifest file and then refresh the installed PWA. On some OSes, you may need to uninstall and reinstall the app to force a refresh, but usually a simple refresh (e.g., closing and reopening the app) works.