Input
Pyxen supports keyboard, touch, mouse, and gamepad input. All input is read through the input module and updated once per frame, before update() runs.
Keyboard
Access any key by name on input.keyboard:
if input.keyboard.right.down:
player.x += 2
if input.keyboard.space.pressed:
jump()
Every key exposes the same properties:
| Property | Meaning |
|---|---|
down | Held down this frame |
pressed | Just pressed this frame (true for one frame) |
released | Just released this frame (true for one frame) |
value | Analog value (0.0 or 1.0 for keys) |
Common key names: left, right, up, down, space, enter, escape, a–z, 0–9.
Touch
On iPad, touch input comes through input.touches — a list of active touch points:
for touch in input.touches:
if touch.started:
spawn_particle(touch.x, touch.y)
Each touch has:
| Property | Meaning |
|---|---|
x, y | Position in game coordinates |
dx, dy | Movement since last frame |
pressure | Touch pressure (if available) |
down | Currently touching |
started | Touch began this frame |
ended | Touch ended this frame |
Mouse
Mouse input works on Mac and iPad with a connected mouse or trackpad:
if input.mouse.buttons.left.pressed:
shoot(input.mouse.x, input.mouse.y)
The mouse exposes:
x,y— cursor position in game coordinatesdx,dy— movement since last framewheel— scroll wheel deltabuttons.left,buttons.right,buttons.middle— each withdown,pressed,released
Gamepads
Pyxen supports up to 4 gamepads. Access them through input.gamepads:
pad = input.gamepads[0]
if pad.connected:
player.x += pad.left_stick.x * 2
if pad.buttons.a.pressed:
jump()
Each gamepad has:
connected— whether a gamepad is plugged in at this indexleft_stick,right_stick— analog sticks withxandy(-1.0 to 1.0)buttons—a,b,x,y, plus triggers and bumpers, each withdown,pressed,released,value
Designing for multiple inputs
A Pyxen game can run on iPad with touch, on iPad with a keyboard, or on Mac with mouse and keyboard. A good approach is to check multiple input sources:
def update():
dx = 0
if input.keyboard.right.down:
dx = 2
elif input.keyboard.left.down:
dx = -2
pad = input.gamepads[0]
if pad.connected:
dx = pad.left_stick.x * 2
player.x += dx
For the full API, see the input reference.