Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

keyboard arrow event 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);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript canvas touchmove 
Javascript :: bubble sort javascript 
Javascript :: stop a video jquery 
Javascript :: fetch await 
Javascript :: js generate id 
Javascript :: How to make string shorter javascript 
Javascript :: multer file type validation 
Javascript :: jquery transform scale 
Javascript :: how to disable a div in javascript 
Javascript :: getting the current url in node js 
Javascript :: jquery get origin url 
Javascript :: open link in new window chrome mac shortcut 
Javascript :: boton de copiar en html y js 
Javascript :: add quotes to array values javascript 
Javascript :: TypeError: Promise resolver undefined is not a function 
Javascript :: how to add multiple css style in javascript 
Javascript :: createstore is deprecated 
Javascript :: javascript random boolean 
Javascript :: ejs with express 
Javascript :: jquery grid get all selected row 
Javascript :: submit form without loading page 
Javascript :: reactjs onclick open new page 
Javascript :: activate es6 module node package.json 
Javascript :: js create array with n elements 
Javascript :: jquery check input 
Javascript :: how to get date time day year in javscript 
Javascript :: Add table row in jQuery 
Javascript :: scroll to top jquery 
Javascript :: reactive localstorage in react 
Javascript :: react font awesome npm 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =