Workshop › Pinball › Step 8 of 42
Space (or the Down arrow) launches the ball: it gets a speed of 16 pixels a frame upwards.
```js
function launch() {
ball.vy = -16
}
document.addEventListener('keydown', (event) => {
if (event.key === ' ' || event.key === 'ArrowDown') {
event.preventDefault()
launch()
}
})
```launch() sets vy to -16: sixteen pixels up every frame.addEventListener('keydown', ...) runs the arrow function each time a key goes down; event.key is its name.' ' is Space; || means "or". preventDefault() stops the browser from scrolling the page with these keys.Write launch and the key listener above function draw() {, then Run, click the game and press Space.
Skills: Keyboard and mouse input
← Move by the velocity Gravity →