Workshop › Pong › Step 8 of 26
A paddle should move while its key is held, and two players press keys at the same time. So we keep a note of
every key that is down: keydown writes true, keyup writes false.
```js
const keys = {}
document.addEventListener('keydown', (event) => {
keys[event.key] = true
})
document.addEventListener('keyup', (event) => {
keys[event.key] = false
})
```keys is an empty object that will hold one entry per key: keys.w is true while W is down.addEventListener('keydown', ...) runs the function whenever a key goes down; event.key is its name.keys[event.key] = true writes into the entry whose name is in event.key.Under the right line, write keys and, after an empty line, the two listeners. Press Run.
Skills: Keyboard and mouse input
← The game loop Move the paddles →