Lesson 5 of 7
Drive with the keyboard
document.addEventListener('keydown', …) listens to the keyboard: the received event contains event.key ('ArrowRight', 'ArrowLeft'…).
An if on event.key plus your moveCar function does the rest — your car answers to the arrows!
document.addEventListener('keydown', function (event) {
if (event.key === 'ArrowRight') {
moveCar(10);
}
});🎯 Your goal
Listen to the keyboard: the right arrow (ArrowRight) calls moveCar(10), the left arrow (ArrowLeft) calls moveCar(-10).
PRO lesson
This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.
Discover PRO →