Polyfills the accessKeyLabel property to improve discoverability of native keyboard shortcuts for your website. Tiny (< 1KB) and dependency-free.
With the accesskey
HTML property,
you can easily provide keyboard shortcuts for the buttons of your web application.
<button accesskey="D">Do crazy stuff</button>
<!-- Can be activated using Ctrl + Alt + D -->
Sadly, most users don't even know these shortcuts can be used, even if the web developer provided
them. To make these shortcuts more discoverable you might decide to use the title
property, a
tooltip or some other UI element to display the keyboard shortcut.
<button accesskey="D">Do crazy stuff</button>
<small>Hint: Use Ctrl + Alt + D to quickly access this button!</small>
A good idea, but browsers use different combinations of modifier keys. But thanks to the
accessKeyLabel
property,
we can easily get a string representation of the keyboard shortcut assigned by the browser. Sooo,
we're good, right? Alas, not quite. Though the API has been around for years,
browser support isn't good enough. Especially, since
Chrome is still missing out. To help bridge the gap, you can use this polyfill and finally let your
users benefit from the keyboard shortcuts you provide!
Full length article about accessKey and accessKeyLabel on dev.to
Simply install the polyfill:
npm i access-key-label-polyfill
# or
yarn add access-key-label-polyfill
And the import and run the install function:
import "access-key-label-polyfill";
Note: Starting with v1.0.0, this polyfill is a native ES module.
You can also use this polyfill 'oldschool'. Simply download the package files and add a script tag to your body:
<body>
<!-- ... -->
<script type="module" src="path/to/AccessKeyLabelPolyfill.js"></script>
</body>
You can view the demo here: https://tillsanders.github.io/access-key-label-polyfill/
The demo will tell you wether the polyfill detected native support for accessKeyLabel
or not and
will display either output below the button.
First, the polyfill will try to detect wether the browser already supports accessKeyLabel
. If it
does, it will exit immediately. If the browser doesn't support this API, the polyfill will add a
small function to fill the gap. This function will try to detect the browser / operating system and
return the correct label. You can then use accessKeyLabel
as expected.
- To return the correct label, the polyfill needs to determine the correct operating system, browser and sometimes even the browser version. This is done with a very lightweight script and based on the user-agent. While this has been tested successfully and should work reliably, wrong user-agents can still mess it up, of course.
- The polyfill currently supports the major browsers on macOS (including Firefox < v14), namely Chrome. As well as Internet Explorer, Edge and Safari on iOS / iPadOS. There might be more browsers, that need this polyfill, but there is surprisingly little information regarding support, so please feel free to open an issue if you come across an unsupported case.
- Chrome on Android does not seem to support
accessKey
, or at least, I was unable to guess (why is this not documented anywhere?!) the correct modifier keys. Would love to get help with this!
- BREAKING: Convert to native ESM module (remove UMD support)
- BREAKING: Remove installation function (
accessKeyLabelPolyfill()
) and install automatically instead - Add support for Chrome on Windows
- Update dependencies and overhaul tooling
- Improve CI/CD
- Move to Vite + Vitest
- Update demo and documentation
- Fix browser detection of Edge
- Initial implementation