keyDown
Canonical ID: input.keyDown. Nederlands: toetsIngedrukt.
Asks whether a key is held down right now and returns a Boolean: true or false. Repeatedly asking for the current state is called polling. It does not wait for a new press or create a handler that listens for future presses. For a block that starts at the moment a key is pressed, use keyPressed.
The host, the app running the program, supplies the key state. The portable game names are "left", "right", "up", "down", and "space". Native graphical hosts also support common control keys and basic Latin letters and digits (ASCII). A key the host does not provide is reported as not down. Key names are text data and stay the same when you translate source syntax.
Signature
keyDown(key)
Arguments
| Parameter | Type | Requirement |
|---|---|---|
key |
Text | Nonempty host key name; data values are not translated. |
Return value
Boolean.
Example
# language: en
when started():
let x = 320
for frame in repeatList(50, none):
if keyDown("left"):
set x = x - 2
clearCanvas("white")
circle(x, 180, 15, "blue")
wait(0.04)
For about two seconds, holding Left moves the circle left in a graphical host.
Notes and common mistakes
Use the function name for your source language. Its canonical ID is a shared documentation label; you do not type that ID as a call. Giving your own variable or function the same name can hide the built-in. Supply the documented arguments and value types. Pliro reports mismatches when checking can detect them, or when the call runs.
The selected host mediates input. Program replies are bounded to 64 KiB of valid UTF-8; unavailable input produces E4010. Keyboard names and option strings are data and are not translated with source syntax. Completed prompts are cooperative checkpoints.
Related entries
ask · askNumber · askYesNo · choose · Programming guides · Host support · Calls and arguments
