Pencil Code Reference > keydown

 

keydown do something when a key is pressed

keydown creates a function that will run when a certain key is pressed.

Syntax

keydown 'w', ->
  ...
  ...
  ...

keydown can be used to execute functions in a script.

In the following script, pressing 1 runs pen red, 2 runs pen green, and 3 runs pen blue. The turtle moves to wherever the mouse is clicked using click.

pen red
keydown '1', ->
  pen red
keydown '2', ->
  pen green
keydown '3', ->
  pen blue
click (e) ->
  moveto e