Workshop › Sokoban › Step 14 of 29
Arrow keys call move. A lookup object maps each key name to its [dx, dy], and after every key the picture is
drawn again. Sokoban is turn-based, so it needs no game loop.
```js
const directions = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
document.addEventListener('keydown', (event) => {
if (directions[event.key]) {
event.preventDefault()
move(...directions[event.key])
}
draw()
})
```directions is a lookup object: directions['ArrowUp'] is [0, -1]. For any other key it is undefined, so the
if asks "is this an arrow?".event.preventDefault() stops the arrows from scrolling the page.... spreads the pair into two arguments: move(0, -1).draw() shows the new state after every key.Above function draw() {, write directions and the listener, and leave an empty line. Press Run, click the game
and use the arrows.
Skills: Keyboard and mouse input
← One step Boxes are in the way →