انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

keydown: move the robot with the arrows

Section 7: Events and formsLesson 7.2 · ⏱ 9 min

keydown works when you press any key on the keyboard, and e.key tells us which key it is.

HTML
<p>Click the page, then use the arrows ⬅️ ➡️ ⬆️ ⬇️</p>
<div id="robot">🤖</div>
CSS
body { font-family: Tahoma, sans-serif; height: 230px; }
#robot { position: absolute; left: 20px; top: 60px; font-size: 40px; }
JavaScript
let x = 20, y = 60;
const robot = document.getElementById("robot");

document.addEventListener("keydown", function (e) {
  if (e.key === "ArrowRight") x += 20;
  if (e.key === "ArrowLeft")  x -= 20;
  if (e.key === "ArrowDown")  y += 20;
  if (e.key === "ArrowUp")    y -= 20;
  robot.style.left = x + "px";
  robot.style.top = y + "px";
});
🖥️ The page in Robo Web:The page in Robo Web
🖥️ What happens on the stage?
We pressed ➡️ eight times and ⬇️ three times, and the robot moved 160 pixels right and 60 pixels down.
💡 Tip
Click the page first so it hears the keyboard.
  1. Stop the robot going out on the left: don't let x be less than 0.

📌 Another example: type the letter

You press a letter on the keyboard, it appears big, and the counter goes up.

🌐 Type the letter
HTML
<p>Click the page, then type any letter.</p>
<div id="big">?</div>
<p>You pressed <b id="n">0</b> times</p>
CSS
body { font-family: Tahoma, sans-serif; height: 230px; }
#big { font-size: 90px; text-align: center; }
JavaScript
let n = 0;
document.addEventListener("keydown", function (e) {
  n++;
  document.getElementById("big").textContent = e.key;
  document.getElementById("n").textContent = n;
});
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Space changes the background colour.

👀 Show the solution
🌐 Solution
HTML
<p>Click the page, then Space</p>
CSS
body { font-family: Tahoma, sans-serif; height: 230px; }
JavaScript
document.addEventListener("keydown", function (e) {
  if (e.key === " ") document.body.style.background = "gold";
});
🖥️ The page in Robo Web:The page in Robo Web

Result: a gold background.

Medium

The ⬆ and ⬇ arrows make the balloon bigger and smaller.

👀 Show the solution
🌐 Solution
HTML
<p>Click the page, then ⬆ ⬇</p>
<div id="b">🎈</div>
CSS
body { font-family: Tahoma, sans-serif; height: 230px; }
JavaScript
let size = 40;
const b = document.getElementById("b");
document.addEventListener("keydown", function (e) {
  if (e.key === "ArrowUp") size += 10;
  if (e.key === "ArrowDown") size -= 10;
  b.style.fontSize = size + "px";
});
🖥️ The page in Robo Web:The page in Robo Web

Result: the balloon became 70px.

Hard

The robot moves with the arrows and doesn't go out on the left (x is never less than 0).

👀 Show the solution
🌐 Solution
HTML
<p>Click the page, then the arrows</p>
<div id="robot">🤖</div>
CSS
body { font-family: Tahoma, sans-serif; height: 230px; }
#robot { position: absolute; left: 0; top: 60px; font-size: 40px; }
JavaScript
let x = 0;
const robot = document.getElementById("robot");
document.addEventListener("keydown", function (e) {
  if (e.key === "ArrowRight") x += 20;
  if (e.key === "ArrowLeft") x -= 20;
  if (x < 0) x = 0;
  robot.style.left = x + "px";
});
🖥️ The page in Robo Web:The page in Robo Web

Result: it stopped at 0, then 20px.

Finished the lesson? Mark it so your progress counts.