Workshop › Conway's Game of Life › Step 20 of 32
The keys now just name a button: a small table turns Space into 'Play' and N into 'Step', and press does the
rest.
```js
document.addEventListener('keydown', (event) => {
const keys = { ' ': 'Play', n: 'Step' }
const button = keys[event.key.toLowerCase()]
if (!button) return
event.preventDefault()
press(button)
})
```{ ' ': 'Play', n: 'Step' } is an object used as a table: keys['n'] is 'Step', keys[' '] (Space) is 'Play'.
A key that is not in it gives undefined.if (!button) return stops for keys we do not use.event.preventDefault() stops the browser's own action, like Space scrolling the page.Inside the keydown listener, replace the if (... === 'n') step() line with the five new lines.
Skills: Keyboard and mouse input
← One place for the buttons Clear and shuffle →