Workshop › Pong › Step 8 of 26

Remember held keys

What we are doing

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
})
```

What it means

Your turn

Under the right line, write keys and, after an empty line, the two listeners. Press Run.

Skills: Keyboard and mouse input

All steps

  1. Get the canvas and the pen
  2. A black court
  3. One dash of the net
  4. The whole net
  5. Two paddles
  6. A function that draws
  7. The game loop
  8. Remember held keys
  9. Move the paddles
  10. Stay on the court
  11. The ball
  12. The ball moves
  13. Bounce off the top and bottom
  14. Does the ball touch a paddle?
  15. The paddles hit back
  16. Serve again
  17. Keep the score
  18. Show the score
  19. First to five
  20. Who won?
  21. Play again
  22. Aim with the paddle
  23. Faster every hit
  24. A computer opponent
  25. One player or two
  26. Build it yourself: a serve pause