Pencil Code Reference > click
click
is an event-handler function. When a user clicks on the screen with the mouse, a function is called and an action is performed.click (e) -> #line 1 #line 2 #line 3 #...
e
is the location the mouse was clicked. This can be used as a location in commands such as moveto
, jumpto
, turnto
, etc.click
preforms like a regular function, but it cannot be called within the code and cannot be given a specific input. It is only called when the user clicks the mouse. The corresponding action can be anything you like, e.g. exit a program, run a script, draw something.
In this example, clicking on the screen moves the turtle to the position that was clicked.
Notice that we have specified speed Infinity
in order to keep up with the speed of the mouse clicks.
speed Infinity
click (e) -> moveto e
But click
does not have to use the location of the mouse at all.
The following example counts the number of times the mouse is clicked, regardless of location.
speed Infinity ht() a = 0 dot lightblue, 10000 label a, { fontSize: 60, fontFamily: "Consolas" } click -> cs() dot lightblue, 10000 a += 1 label a, { fontSize: 60, fontFamily: "Consolas" }
Note that, in the above example, if you click fast enough, you can actually select the label. This is because a label
is actually an HTML element, just like the rest of the text on this page.