Input¶
key_pressed¶
- key_pressed(key)¶
Check whether a keyboard key is currently being held down.
- Parameters:
key (string) – Key name from the browser keyboard event.
- Returns:
trueif the key is pressed,falseotherwise.
This function uses the browser’s
event.keyvalues, so the exact string matters.
Common key values¶
Key |
String |
|---|---|
Left arrow |
|
Right arrow |
|
Up arrow |
|
Down arrow |
|
Space bar |
|
A key |
|
D key |
|
W key |
|
S key |
|
Warning
Key names are case-sensitive. "ArrowLeft" works, but "arrowleft" does not.
Examples¶
-- Arrow key movement
if key_pressed("ArrowLeft") then
player.x = player.x - 2
end
-- WASD movement
if key_pressed("a") then player.x = player.x - 2 end
if key_pressed("d") then player.x = player.x + 2 end
if key_pressed("w") then player.y = player.y - 2 end
if key_pressed("s") then player.y = player.y + 2 end
-- Space bar action
if key_pressed(" ") then
jump()
end