Skip to content

Instantly share code, notes, and snippets.

@sneakers-the-rat
Last active January 5, 2022 09:12
Show Gist options
  • Save sneakers-the-rat/08866f7f4c6e2d6ba467c2e31058a1d5 to your computer and use it in GitHub Desktop.
Save sneakers-the-rat/08866f7f4c6e2d6ba467c2e31058a1d5 to your computer and use it in GitHub Desktop.
gate mouse events
// define key that toggles class
const ctrlKeys = ['CapsLock', 'ControlRight'];
// make element to block pointer events
let blockStyle = document.createElement('style')
blockStyle.type = "text/css"
blockStyle.innerText = "* {pointer-events: none !important;}"
// register callback to detect keydown events to gate
document.addEventListener('keydown', (e) => {
if (ctrlKeys.includes(e.code)) {
blockStyle.remove();
}
})
// remove class when commandkeyup
document.addEventListener('keyup', (e) => {
if (ctrlKeys.includes(e.code)) {
document.head.appendChild(blockStyle);
}
})
// append class to document head on load
window.addEventListener('DOMContentLoaded', (e) => {
document.head.appendChild(blockStyle);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment