<!DOCTYPE HTML>
<html>
<body>
<p>Press "Q" and "W" together (can be in any language).</p>
<script>
function runOnKeys(func, ...codes){ // func = a function returns hello, codes = q and w key
let pressed = new Set();
document.addEventListener('keydown', function(event) {
pressed.add(event.code);
for (let code of codes) { // from spread parameter
if(!pressed.has(code)) {
return;//if the key is neither q nor w, return
}
}
pressed.clear();
func();
});
document.addEventListener('keyup', function(event){
pressed.delete(event.code);
});
}
runOnKeys(
()=> alert("Hello!"),
"KeyQ",
"KeyW"
);
</script>
<body>
</html>
<html>
<body>
<p>Press "Q" and "W" together (can be in any language).</p>
<script>
function runOnKeys(func, ...codes){ // func = a function returns hello, codes = q and w key
let pressed = new Set();
document.addEventListener('keydown', function(event) {
pressed.add(event.code);
for (let code of codes) { // from spread parameter
if(!pressed.has(code)) {
return;//if the key is neither q nor w, return
}
}
pressed.clear();
func();
});
document.addEventListener('keyup', function(event){
pressed.delete(event.code);
});
}
runOnKeys(
()=> alert("Hello!"),
"KeyQ",
"KeyW"
);
</script>
<body>
</html>
댓글
댓글 쓰기