Last active
July 8, 2024 14:57
-
-
Save va9iff/30f77e0cbe9475c3be3caa2bd3dd545b to your computer and use it in GitHub Desktop.
elegant JavaScript keyboard shortcut binder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
function makeBindi(keys) { | |
return new Proxy({}, { | |
get(_, key) { | |
return makeBindi([...keys, key]) | |
}, | |
set(_, key, func) { | |
document.addEventListener('keydown', e => { | |
if ( | |
(e.key.toLowerCase() === key) && | |
(keys.includes("alt") ? e.altKey : true) && | |
(keys.includes("shift") ? e.shiftKey : true) && | |
(keys.includes("ctrl") ? e.ctrlKey : true) | |
) func(e) | |
}) | |
return true | |
}, | |
}) | |
} | |
window.bindi = new Proxy({}, { | |
get(_, key) { | |
return makeBindi([key]) | |
}, | |
}) | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.bindi.ctrl.shift.v = e => alert("hii") | |
window.bindi.ctrl.y = e => alert("yanked it") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment