Last active
October 23, 2023 13:50
-
-
Save nexpr/19b985317b325ad5a10cbc9a70ad7376 to your computer and use it in GitHub Desktop.
navigator.keyboard.lock https://gistcdn.githack.com/nexpr/19b985317b325ad5a10cbc9a70ad7376/raw/16e7e685b24b53108a547713ab24969600532db4/keyboard-lock-test.html
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
<!doctype html> | |
<meta charset="utf-8" /> | |
<script type="module"> | |
const addLog = (key) => { | |
const div = document.createElement("div") | |
div.textContent = `key: ${key}` | |
setTimeout(() => { | |
div.animate([ | |
{ height: div.clientHeight + "px" }, | |
{ height: "0" }, | |
], 300).finished.then(() => div.remove()) | |
}, 5000) | |
log.prepend(div) | |
} | |
window.addEventListener("keydown", (event) => { | |
event.preventDefault() | |
console.log("key:", event.key) | |
addLog(event.key) | |
}) | |
fullscreen.onclick = async () => { | |
if (document.fullscreenElement) { | |
await document.exitFullscreen() | |
} else { | |
await document.documentElement.requestFullscreen() | |
} | |
} | |
document.addEventListener("fullscreenchange", () => { | |
if (document.fullscreenElement) { | |
fullscreen.classList.add("on") | |
} else { | |
fullscreen.classList.remove("on") | |
} | |
}) | |
keyboardlock.onclick = async () => { | |
if (keyboardlock.classList.contains("on")) { | |
await navigator.keyboard.unlock() | |
keyboardlock.classList.remove("on") | |
} else { | |
await navigator.keyboard.lock() | |
keyboardlock.classList.add("on") | |
} | |
} | |
</script> | |
<style> | |
body { | |
width: 80%; | |
margin: auto; | |
display: flex; | |
flex-flow: column; | |
padding: 20px; | |
gap: 20px; | |
} | |
button { | |
padding: 10px; | |
&.on { | |
background: yellow; | |
} | |
} | |
#notice { | |
border: 1px dotted #5aa; | |
background: #dff; | |
padding: 10px; | |
} | |
#log > div { | |
font-size: 11px; | |
overflow: hidden; | |
} | |
</style> | |
<button id="fullscreen">フルスクリーン</button> | |
<button id="keyboardlock">キーボードロック</button> | |
<div id="notice">キーボードロック時のフルスクリーンの解除は Escape キーの長押し</div> | |
<div id="log"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment