tiramisu/input
Input module - keyboard, mouse, gamepad, and touch input handling.
Input state is automatically updated each frame and passed to your update function
via the Context. Query the input state to respond to player actions.
Quick Example
import tiramisu/input
fn update(model, msg, ctx) {
// Check if player is pressing W to move forward
let move_forward = case input.is_key_pressed(ctx.input, input.KeyW) {
True -> 1.0
False -> 0.0
}
// Check mouse button
let shooting = input.is_left_button_pressed(ctx.input)
// Update model based on input
Model(..model, position: move_player(model.position, move_forward))
}
Types
pub type ButtonState {
ButtonState(
pressed: Bool,
just_pressed: Bool,
just_released: Bool,
)
}
Constructors
-
ButtonState( pressed: Bool, just_pressed: Bool, just_released: Bool, )
pub type GamepadAxis {
LeftStickX
LeftStickY
RightStickX
RightStickY
}
Constructors
-
LeftStickX -
LeftStickY -
RightStickX -
RightStickY
Gamepad button enumeration (standard mapping)
pub type GamepadButton {
ButtonA
ButtonB
ButtonX
ButtonY
LeftBumper
RightBumper
LeftTrigger
RightTrigger
Select
Start
LeftStick
RightStick
DPadUp
DPadDown
DPadLeft
DPadRight
HomeButton
}
Constructors
-
ButtonA -
ButtonB -
ButtonX -
ButtonY -
LeftBumper -
RightBumper -
LeftTrigger -
RightTrigger -
Select -
Start -
LeftStick -
RightStick -
DPadUp -
DPadDown -
DPadLeft -
DPadRight -
HomeButton
pub type GamepadState {
GamepadState(
connected: Bool,
buttons: List(Float),
axes: List(Float),
)
}
Constructors
-
GamepadState( connected: Bool, buttons: List(Float), axes: List(Float), )Arguments
- buttons
-
Buttons: Values clamped between 0.0 and 1.0
Input state for all input devices (automatically updated each frame).
Access via context.input in your update function.
pub opaque type InputState
Common keyboard codes (standard KeyboardEvent.code values)
pub type Key {
KeyA
KeyB
KeyC
KeyD
KeyE
KeyF
KeyG
KeyH
KeyI
KeyJ
KeyK
KeyL
KeyM
KeyN
KeyO
KeyP
KeyQ
KeyR
KeyS
KeyT
KeyU
KeyV
KeyW
KeyX
KeyY
KeyZ
Digit0
Digit1
Digit2
Digit3
Digit4
Digit5
Digit6
Digit7
Digit8
Digit9
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
ArrowUp
ArrowDown
ArrowLeft
ArrowRight
ShiftLeft
ShiftRight
ControlLeft
ControlRight
AltLeft
AltRight
MetaLeft
MetaRight
Space
Enter
Escape
Tab
Backspace
Delete
Insert
Home
End
PageUp
PageDown
CapsLock
Minus
Equal
BracketLeft
BracketRight
Backslash
Semicolon
Quote
Comma
Period
Slash
Backquote
Numpad0
Numpad1
Numpad2
Numpad3
Numpad4
Numpad5
Numpad6
Numpad7
Numpad8
Numpad9
NumpadAdd
NumpadSubtract
NumpadMultiply
NumpadDivide
NumpadDecimal
NumpadEnter
NumLock
AudioVolumeUp
AudioVolumeDown
AudioVolumeMute
MediaPlayPause
MediaStop
MediaTrackNext
MediaTrackPrevious
PrintScreen
ScrollLock
Pause
ContextMenu
Custom(String)
}
Constructors
-
KeyA -
KeyB -
KeyC -
KeyD -
KeyE -
KeyF -
KeyG -
KeyH -
KeyI -
KeyJ -
KeyK -
KeyL -
KeyM -
KeyN -
KeyO -
KeyP -
KeyQ -
KeyR -
KeyS -
KeyT -
KeyU -
KeyV -
KeyW -
KeyX -
KeyY -
KeyZ -
Digit0 -
Digit1 -
Digit2 -
Digit3 -
Digit4 -
Digit5 -
Digit6 -
Digit7 -
Digit8 -
Digit9 -
F1 -
F2 -
F3 -
F4 -
F5 -
F6 -
F7 -
F8 -
F9 -
F10 -
F11 -
F12 -
ArrowUp -
ArrowDown -
ArrowLeft -
ArrowRight -
ShiftLeft -
ShiftRight -
ControlLeft -
ControlRight -
AltLeft -
AltRight -
MetaLeft -
MetaRight -
Space -
Enter -
Escape -
Tab -
Backspace -
Delete -
Insert -
Home -
End -
PageUp -
PageDown -
CapsLock -
Minus -
Equal -
BracketLeft -
BracketRight -
Backslash -
Semicolon -
Quote -
Comma -
Period -
Slash -
Backquote -
Numpad0 -
Numpad1 -
Numpad2 -
Numpad3 -
Numpad4 -
Numpad5 -
Numpad6 -
Numpad7 -
Numpad8 -
Numpad9 -
NumpadAdd -
NumpadSubtract -
NumpadMultiply -
NumpadDivide -
NumpadDecimal -
NumpadEnter -
NumLock -
AudioVolumeUp -
AudioVolumeDown -
AudioVolumeMute -
MediaPlayPause -
MediaStop -
MediaTrackNext -
MediaTrackPrevious -
PrintScreen -
ScrollLock -
Pause -
ContextMenu -
Custom(String)
pub opaque type KeyboardState
pub opaque type MouseState
pub type Touch {
Touch(id: Int, x: Float, y: Float)
}
Constructors
-
Touch(id: Int, x: Float, y: Float)
Values
pub fn gamepad_axis(
input: InputState,
gamepad_index: Int,
axis: GamepadAxis,
) -> Float
Get gamepad axis value
pub fn gamepad_button(
input: InputState,
gamepad_index: Int,
button: GamepadButton,
) -> Float
Get gamepad button value
pub fn get_axis_with_deadzone(
input: InputState,
gamepad_index: Int,
axis: GamepadAxis,
deadzone: Float,
) -> Float
Get axis value with dead zone applied
pub fn get_primary_axis(
input: InputState,
axis: GamepadAxis,
) -> Float
Convenience: Get axis value on primary gamepad
pub fn get_primary_button(
input: InputState,
button: GamepadButton,
) -> Float
Convenience: Get button value on primary gamepad
pub fn is_gamepad_button_pressed(
input: InputState,
gamepad_index: Int,
button: GamepadButton,
) -> Bool
Check if gamepad button is pressed
pub fn is_gamepad_connected(
input: InputState,
index: Int,
) -> Bool
Check if gamepad at index is connected
pub fn is_key_just_pressed(input: InputState, key: Key) -> Bool
Check if a key was just pressed this frame
pub fn is_key_just_released(input: InputState, key: Key) -> Bool
Check if a key was just released this frame
pub fn is_key_pressed(input: InputState, key: Key) -> Bool
Check if a key is currently pressed
pub fn is_left_button_just_pressed(input: InputState) -> Bool
Check if left mouse button was just pressed
pub fn is_left_button_pressed(input: InputState) -> Bool
Check if left mouse button is pressed
pub fn is_left_stick_active(
input: InputState,
gamepad_index: Int,
threshold: Float,
) -> Bool
Check if left stick is moved in any direction
pub fn is_primary_connected(input: InputState) -> Bool
Convenience: Check if primary gamepad (index 0) is connected
pub fn is_primary_gamepad_button_pressed(
input: InputState,
button: GamepadButton,
) -> Bool
Convenience: Check button on primary gamepad
pub fn is_right_button_just_pressed(input: InputState) -> Bool
Check if right mouse button was just pressed
pub fn is_right_button_pressed(input: InputState) -> Bool
Check if right mouse button is pressed
pub fn is_right_stick_active(
input: InputState,
gamepad_index: Int,
threshold: Float,
) -> Bool
Check if right stick is moved in any direction
pub fn new() -> InputState
pub fn touches_just_ended(input: InputState) -> List(Touch)
Get touches that just ended
pub fn touches_just_started(input: InputState) -> List(Touch)
Get touches that just started