Tastaturpfeilereignis Handling JavaScript
// function responding to keyboard event
function control(e) {
if (e.key !== undefined) {
const pressedKey = e.key;
switch (pressedKey) {
case "ArrowLeft":
console.log("Left arrow pressed");
break;
case "ArrowUp":
console.log("Up arrow pressed");
break;
case "ArrowRight":
console.log("Right arrow pressed");
break;
case "ArrowDown":
console.log("Down arrow pressed");
}
// or alternatively
// if (pressedKey === "ArrowLeft") {
// console.log("Left arrow pressed");
// } else if (pressedKey === "ArrowUp") {
// console.log("Up arrow pressed");
// } else if (pressedKey === "ArrowRight") {
// console.log("Right arrow pressed");
// } else if (pressedKey === "ArrowDown") {
// console.log("Down arrow pressed");
}
}
// Add event listener to deal with keyboard events through the control function
document.addEventListener("keyup", control);
Wissam