Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
how can i enter keyboard events

I was working on this project of UFO, Now I want to move this UFO by using arrow keys Up, Down ,left , right. I am unable to do so

function Up() {
    if ((pos) >=10) {
      pos -= 5;
      document.querySelector
      document.querySelector(`img`).style.top = pos + "px";
    }
  }

  function Down() {
    if ((pos) <=870) {
      pos += 5;
      document.querySelector(`img`).style.top = pos + "px";
    }
  }
  function Left() {
    if ((posLeft) >= 10) {
      posLeft -= 5;
      document.querySelector(`img`).style.left = posLeft + "px";
    }
  }
  function Right() {
    if ((posLeft) <= 1880) {
      posLeft += 5;
      document.querySelector(`img`).style.left = posLeft + "px";
    }
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

To add keyboard event handlers you can do the following, after your function declarations:

document.addEventListener("keydown", (e) => {
  switch(e.code){
    case "ArrowUp":
      Up();
      break;
    case "ArrowLeft":
      Left();
      break;
    case "ArrowRight":
      Right();
      break;
    case "ArrowDown":
      Down();
      break;
  }
})

many tutorials or articles online will point you towards KeyboardEvent.keyCode, but it has been deprecated and shouldn't hence be used.

about 4 years ago · Juan Pablo Isaza Report

0

To prevent the annoying delay when initially holding down a key, you could add the current key to an array, and remove it when you're finished with it. Here is how you could do it...

// Array of currently pressed keys
const keys = [];

// Attempt to add pressed key
document.addEventListener("keydown", (e) => {
  if (!keys.includes(e.key)) { // If key isn't already pressed
    keys.push(e.key); // Add key to array
  }
});

// Attempt to remove pressed key
document.addEventListener("keyup", (e) => {
  if (keys.includes(e.key)) { // If key is already pressed
    keys.splice(keys.indexOf(e.key), 1); // Remove key from array
  }
});

// Move UFO based on current pressed keys every 100ms
setInterval(() => {   
  if (keys.includes("w")) { // If the 'keys' array contains 'w', then move up
    // Move up!
    Up();
  }
  if (keys.includes("s")) {
    // Move down!
    Down();
  }
  if (keys.includes("a")) {
    // Move left!
    Left();
  }
  if (keys.includes("d")) {
    // Move right!
    Right();
  }
}, 100);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!