Workshop › Snake › Step 11 of 28
Now the player takes control: when an arrow key is pressed, we change dir.
```js
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowUp') dir = { x: 0, y: -1 }
if (event.key === 'ArrowDown') dir = { x: 0, y: 1 }
if (event.key === 'ArrowLeft') dir = { x: -1, y: 0 }
if (event.key === 'ArrowRight') dir = { x: 1, y: 0 }
})
```addEventListener('keydown', ...) runs the function each time a key goes down.event.key is the key's name, such as 'ArrowUp'.if checks one arrow and sets the matching direction. === means "is exactly equal to".Write the listener under let last = 0, after an empty line. Run, click the game and use the arrow keys.
Skills: Keyboard and mouse input
← A direction to move in A body made of a list →