Skip to content

Keys

How a drawing reads the keyboard: a key held down, or a key pressed. Clicking shapes is on its own page: Clicks.


if ... is down

if key rightarrow is down {
  ...
}

Checks whether a key is held down right now. Put it inside a forever to steer something for as long as the key is held:

draw circle car at 400, 300 having radius 20, fill "tomato"
forever {
  if key leftarrow is down { move car by -5, 0 }
  if key rightarrow is down { move car by 5, 0 }
  if key uparrow is down { move car by 0, -5 }
  if key downarrow is down { move car by 0, 5 }
  wait 0.02 seconds
}

Several keys can be held at once, so two players can play on one keyboard. is not down checks that a key isn't held.


when ... is pressed

when key space is pressed {
  ...
}

Runs its lines each time the key is pressed. Holding the key down doesn't press it again.

draw circle ball at 400, 500 having radius 20, fill "gold"
when key space is pressed {
  move ball by 0, -100 in 0.3 seconds
  move ball by 0, 100 in 0.3 seconds
}

Keys

Write The key
key a … key z a letter
key 0 … key 9 a number
key space the space bar
key enter Enter
key leftarrow, key rightarrow, key uparrow, key downarrow the arrows
key delete, key backspace the Delete and Backspace keys

A key in a setting

key space is a value, like a number or a colour, so a variable or a recipe's setting can hold one. Use it without key:

shape paddle having up key w, down key s {
  rectangle bat having width 10, length 80, fill "black"
  forever {
    if up is down { move bat by 0, -5 }
    if down is down { move bat by 0, 5 }
    wait 0.02 seconds
  }
}
draw paddle left at 20, 260
draw paddle right at 770, 260 having up key uparrow, down key downarrow

The left paddle moves with W and S, the right one with the arrows.


Playing with keys

Click the drawing the keys go to the drawing, and the ⌨️ shows beside it
Click the code the keys go back to the code
Arrows and space move things in the drawing instead of scrolling the page
On a tablet there's no keyboard, so keys can't be used yet