method
Input.key
Input.key() -> int
Returns the key currently pressed this frame as a character code, or 0 when no key is down. Compare the result against character literals such as 'w', 'a', 's', 'd', or ' ' to drive movement and actions. Read it in an Input-phase handler so input is handled once per frame before the world updates. Because it reports the key held on this frame rather than a one-shot press event, a common pattern for "press to confirm" is to wait until Input.key() returns 0 (key released) before accepting the next press.
Example
program MoveByKey {
property Position { column: int = 0, row: int = 0 }
model Player { Position }
const TILE_SIZE: int = 16
handler SpawnPlayer phase Start {
spawn Player { Position { column: 5, row: 5 } }
}
handler ReadInput phase Input {
let pressed = Input.key()
for (Position) in query [Position, {Player}] {
if pressed == 'w' { Position.row = Position.row - 1 }
if pressed == 's' { Position.row = Position.row + 1 }
if pressed == 'a' { Position.column = Position.column - 1 }
if pressed == 'd' { Position.column = Position.column + 1 }
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
for (Position) in query [Position, {Player}] {
Screen.fill_rectangle(
x: Position.column * TILE_SIZE,
y: Position.row * TILE_SIZE,
width: TILE_SIZE,
height: TILE_SIZE,
color: Color.LimeGreen)
}
Screen.show()
}
}