Skip to content

Instantly share code, notes, and snippets.

@pongo
Last active July 24, 2025 20:33
Show Gist options
  • Save pongo/fb7dcfad0f6531ff26346721e75869a1 to your computer and use it in GitHub Desktop.
Save pongo/fb7dcfad0f6531ff26346721e75869a1 to your computer and use it in GitHub Desktop.
import { createKeybindingsHandler } from "tinykeys";
// code from https://github.com/jaywcjlove/hotkeys-js/blob/master/src/index.js
function isInput(event: KeyboardEvent) {
const target = event.target as HTMLInputElement;
const { tagName } = target;
const isInput =
tagName === "INPUT" &&
!["checkbox", "radio", "range", "button", "file", "reset", "submit", "color"].includes(
target.type,
);
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
if (
target.isContentEditable ||
((isInput || tagName === "TEXTAREA" || tagName === "SELECT") && !target.readOnly)
) {
return true;
}
return false;
}
function notInput(listener: (event: KeyboardEvent) => void) {
return (event: KeyboardEvent) => {
if (!isInput(event)) {
listener(event);
}
};
}
const handler = createKeybindingsHandler({
KeyQ: notInput((event: KeyboardEvent) => {
event.preventDefault();
event.stopPropagation();
console.log("q");
}),
KeyA: notInput((event: KeyboardEvent) => {
event.preventDefault();
event.stopPropagation();
console.log("a");
}),
});
diff --git a/node_modules/tinykeys/package.json b/node_modules/tinykeys/package.json
index bbda4b2..38fc5a5 100644
--- a/node_modules/tinykeys/package.json
+++ b/node_modules/tinykeys/package.json
@@ -16,7 +16,8 @@
"exports": {
".": {
"import": "./dist/tinykeys.module.js",
- "require": "./dist/tinykeys.js"
+ "require": "./dist/tinykeys.js",
+ "types": "./dist/tinykeys.d.ts"
}
},
"keywords": [
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment