Drupalwoo ☉

Keyboard shortcuts in Drupal with JavaScript

Drupal administration screens are dense with buttons, tabs and filter options, and content editors working across Sydney, Melbourne and Canberra offices often spend a surprising share of the day reaching for the mouse. Keyboard shortcuts in Drupal with JavaScript can shave seconds off every task, turning a long string of clicks into a single key combination that jumps straight to the content list, opens a node form or saves a draft.

The implementation is not difficult, but doing it well takes a little care. A shortcut handler has to respect fields that are being typed into, avoid clashing with browser and operating system combinations, behave sensibly for users who cannot use a mouse, and remain maintainable when a site updates to a newer Drupal major version. The approach below covers the whole path: attaching JavaScript through a Drupal library and behaviour, detecting key presses safely, scoping commands to particular routes and roles, meeting Australian accessibility expectations, and testing on real devices.

Where shortcuts fit in a Drupal workflow

Shortcuts are most valuable for repeat actions that appear in many places. Content editors who publish twenty articles a week benefit from a combo that opens the content listing, another that focuses the title field of a new node, and a third that saves an unpublished draft. Site administrators covering several sites can move between configuration screens and log screens without touching a trackpad.

They also matter for accessibility rather than speed alone. Government sites built on Drupal in Canberra, including those using the GovCMS distribution, are expected to conform with the Australian Government Web Content Accessibility Guidelines and WCAG 2.1 AA. Success Criterion 2.1.1 requires all functionality to be operable from the keyboard, and shortcuts are one way to make administrative tooling usable for people with limited motor control or who use switch devices and screen readers.

There are several ways to build this in Drupal, and the right choice depends on your core version, the number of commands and how much the shortcuts need to change between screens.

Approach Core version support Build effort Best suited to Watch out for
Vanilla JS behaviour plus drupalSettings Drupal 10 and 11 Low Site-wide commands such as opening the content list You must ignore editable fields yourself
jQuery keydown listener Drupal 9 and 10 Low Legacy sites still shipping jQuery jQuery is no longer present in Drupal 11 core
Behaviour driven by data attributes on forms Drupal 9 to 11 Medium Form actions such as save and publish Requires template and markup changes
Editor plugin inside the WYSIWYG Drupal 9 and 10 Medium Rich text formatting commands CKEditor 5 in Drupal 11 uses a different plugin API
Third-party hotkey library Any version Medium Command palettes and multi-key chords Extra dependency and bundle weight

Attaching JavaScript through a Drupal library and behaviour

The cleanest Drupal pattern is to ship your shortcut code as a library, pass the key combinations through drupalSettings, and attach the behaviour from a Drupal.behaviors.yml file. That keeps configuration out of the JavaScript file, so an editor can add or remove a command without touching code.

A small module can define the library and the settings in two hooks:

function shortcut_library() {
  $libraries['shortcut'] = [
    'css' => [],
    'js' => [
      'js/shortcut.js' => [
        'dependencies' => ['core/drupal', 'core/once'],
      ],
    ],
  ];
  return $libraries;
}

function shortcut_preprocess_html(&$variables) {
  $variables['#attached']['library'][] = 'shortcut';
  $variables['#attached']['drupalSettings']['shortcutKeys'] = [
    'combos' => [
      [
        'id' => 'content',
        'key' => 'c',
        'ctrl' => true,
        'shift' => true,
        'label' => 'Ctrl+Shift+C',
        'route' => '/admin/content',
      ],
    ],
  ];
}

The behaviour file then tells Drupal when to run the script:

shortcut_behaviors:
  shortcut_keys:
    attach:
      library: drupal/shortcut

Because the handler listens on document, commands keep working after an AJAX request inserts new markup, which is exactly what happens when Drupal's admin tables reload. If you are on Drupal 10 and still have jQuery in the project, $(document).on('keydown', handler) is a valid alternative, but Drupal 11 removed jQuery from core, so new code should be written in plain JavaScript against the Drupal and drupalSettings globals.

Detecting key presses without hijacking the browser

Matching a key is straightforward, but matching it safely is where most implementations fail. A naive handler fires while someone is typing their password into the login form, or while an editor is writing the word "cat" into a body field. Always exclude editable targets before acting:

const EDITABLE_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];

function isEditable(target) {
  if (!target) {
    return false;
  }
  return EDITABLE_TAGS.includes(target.tagName) || target.isContentEditable;
}

Compare event.key with the character the user pressed, and use event.code only when you deliberately want a physical key regardless of layout. Australian keyboards are predominantly US-style QWERTY, but many Australian developers type on MacBook hardware where the Command key replaces Control, and some colleagues use alternate layouts, so offering both Ctrl and Cmd variants of a command is a thoughtful touch.

Choose combinations that browsers and operating systems have not claimed. Ctrl+T, Ctrl+N, Ctrl+W, Ctrl+Tab and Ctrl+P belong to the browser, and Ctrl+S still triggers "save page" in most desktop browsers, which is why a save-and-publish command is better bound to something such as Ctrl+Shift+Alt+S or scoped to a specific form. If you must offer Ctrl+S for a draft save, check the event target and route before calling preventDefault().

Scoping commands to screens, roles and forms

Not every combination should exist on every page. The content list shortcut makes little sense on a public article, and an administrator command has no business appearing for anonymous visitors. Send the allowed routes or roles through drupalSettings and check them inside the handler before running anything.

In practice it is easier to store a small configuration entity listing each command with its key combination, target route and permitted roles, then render that list into drupalSettings during preprocessing. Editors can then see exactly which commands are active for them. Role checks are useful for usability, but remember they are a convenience layer only: the server still enforces every permission, and a shortcut must never unlock an action that the user could not perform by clicking the interface. If a command performs an action, route it through a menu path or an AJAX callback that Drupal itself authorises, rather than posting to an endpoint that skips access control.

For form-specific actions, mark the form with a data attribute and read it in the behaviour. A publish shortcut that only works when a data-shortcut-publish attribute is present on the current form keeps the behaviour predictable and avoids accidental publishing from unrelated pages.

Accessibility, discoverability and Australian expectations

A shortcut that nobody knows about does not exist. Put a visible help panel or a keyboard shortcut cheat sheet in the administration theme, listing each combination in the same wording used on the page. Australian English spelling is worth honouring here, since "organisation" and "licence" are correct local forms, and if your help screen shows dates, use the day-first format Australians read every day, such as 04/03/2026.

Never make a shortcut the only route to a feature. Every command should have a visible button, and the cheat sheet should be reachable with the keyboard alone. Do not trap focus, and do not move focus unexpectedly when a command fires; if a shortcut opens a dialog, move focus into it and return it afterwards. Announce state changes through an aria-live region where a screen reader user would otherwise hear nothing.

Allow people to turn shortcuts off. Storing a preference on the user entity and checking it in the handler gives keyboard-heavy users a way to keep the combinations they rely on for their own editing software while using Drupal in the same browser. The Australian Government Digital Service Standard, which many public sector Drupal projects are assessed against, expects this kind of user control, and agency accessibility audits in Melbourne and Canberra regularly flag unexpected focus movement and overridden browser keys.

Testing, debugging and maintaining the code

Test on the hardware your editors actually use, not only on your own machine. A quick pass in Chrome and Edge on Windows plus Safari on macOS catches most problems, and a second opinion from colleagues at Drupal Australia meetups in Sydney and Melbourne has surfaced many keyboard layout quirks over the years. When something does not fire, check the console for JavaScript errors, confirm the library loaded on the Network tab, and print drupalSettings.shortcutKeys to verify the settings arrived.

Watch for render caching. When a page is cached, drupalSettings can be stored with it, so a changed combination may appear stale until the cache is cleared. JS aggregation and minification in production can also mask a syntax error that works locally, so build the site in a production-like mode before sign-off.

Keep the behaviour in version control alongside the module, document the available combinations in the site handbook, and revisit them after each Drupal core update, because a new core version can change the admin routes a command points at. For teams maintaining many Drupal sites locally, a maintained configuration file rather than hard-coded JavaScript makes the difference between a five-minute change and an afternoon of hunting through templates.