Workshop › OutRun-style Racer › Step 13 of 44
The arrow keys will be the pedals. We keep the keys that are held down in an object, keys: true while a key is down,
false after it is released. Then update can ask every frame whether up is held.
```js
const keys = {}
document.addEventListener('keydown', (event) => {
keys[event.key] = true
if (event.key.startsWith('Arrow')) event.preventDefault()
})
document.addEventListener('keyup', (event) => {
keys[event.key] = false
})
```addEventListener('keydown', ...) runs the function whenever a key is pressed; event.key is its name, like
'ArrowUp'.keys[event.key] = true writes into the field with that name; keyup writes false.preventDefault() stops the arrow keys from scrolling the page.let speed write const keys = {}.function update() { write the two listeners, followed by an empty line.Skills: Keyboard and mouse input
← Moving is one number The gas pedal →