Workshop › Rhythm Lanes › Step 13 of 35
The keys D, F, J and K (where your fingers rest) or the four arrows press the lanes. A table turns a key into a lane,
and press(lane) lights that lane up.
```js
const KEYS = { d: 0, f: 1, j: 2, k: 3, ArrowLeft: 0, ArrowDown: 1, ArrowUp: 2, ArrowRight: 3 }
function press(lane) {
lit[lane] = 8
}
document.addEventListener('keydown', (event) => {
const lane = KEYS[event.key] ?? KEYS[event.key.toLowerCase()]
if (lane === undefined) return
event.preventDefault()
press(lane)
})
```KEYS is an object used as a table: KEYS['j'] is 2. A key that is not in it gives undefined.press(lane) sets that lane's countdown to 8 frames.addEventListener('keydown', ...) runs the arrow function each time a key goes down; event.key is its name.a ?? b uses b when a is missing: the key as it is, or else in lower case (so a capital D works too).preventDefault() stops the arrow keys from scrolling the page.LEAD write KEYS.function update() { write press, with an empty line after it.function draw() { write the keydown listener, with an empty line after it.Skills: Keyboard and mouse input
← Lanes that light up Show the keys →