Make keyboard shortcut keys look pressed when a user presses a corresponding key.
npm install animated-keyboard-shortcuts --save
# or:
yarn add animated-keyboard-shortcuts<kbd>
<kbd>Ctrl</kbd>
+
<kbd>Shift</kbd>
+
<kbd>F</kbd>
</kbd>import { registerElement } from "animated-keyboard-shortcuts"
const keys = document.querySelectorAll("kbd > kbd")
for (const key of keys) {
registerElement(key)
}/* Styles for a pressed key */
kbd > kbd.pressed {
/* ... */
}-
registerElement(element: HTMLElement): voidSpecify that the element should get a CSS class
pressedwhen a certain key is pressed.If the passed element has an attribute
data-key-name, then its value will be used as the key name.Else if the element has some text inside, then that text will be used as a key name.
Else
RegistrationErrorwill be thrown. -
RegistrationError extends ErrorThis is the error, thrown by
registerElement.Properties:
- same properties as in the
Errorclass element: HTMLElement- the element that caused the error
- same properties as in the
-
addEventListeners(): voidThis is a function that sets up event listeners for
keydownandkeyupevents.You don't have to call it manually, because
registerElementcalls it when needed. -
removeEventListeners(): voidThis is a function to remove event listeners for
keydownandkeyupevents.
This project works directly in the browser without any additional build steps.
- Clone the repository
git clone https://github.com/ivteplo/animated-keyboard-shortcuts- Navigate into the cloned folder
cd animated-keyboard-shortcuts-
Open
index.htmlin the browser -
Happy hacking! 🎉
