Last active
January 5, 2022 09:12
-
-
Save sneakers-the-rat/08866f7f4c6e2d6ba467c2e31058a1d5 to your computer and use it in GitHub Desktop.
gate mouse events
This file contains hidden or 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
// 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