Header menu logo Mibo.Raylib

Input (raw + mapped)

Mibo.Raylib supports input via semantic input mapping (hardware → actions) using InputMap + InputMapper.subscribe.

Subscription-based input is available for keyboard, mouse, touch, gamepad, and gestures. Direct polling is also available via InputPolling.

Semantic input mapping (actions)

Gameplay reads better when it talks about actions (Jump, Fire, Interact) instead of keys. Mibo.Raylib provides InputMap and ActionState for this purpose.

Define your action type

type Action =
    | MoveLeft
    | MoveRight
    | Jump
    | Fire

Build an InputMap

open Raylib_cs
open Mibo.Input

let map =
    InputMap.empty
    |> InputMap.key MoveLeft KeyboardKey.A
    |> InputMap.key MoveLeft KeyboardKey.Left
    |> InputMap.key Jump KeyboardKey.Space

Subscribe with InputMapper.subscribe

The recommended approach uses InputMapper.subscribe to wire your InputMap into an Elmish subscription:

open Mibo.Input

type Msg =
    | InputMapped of ActionState<Action>

let subscribe (ctx: GameContext) (model: Model) : Sub<Msg> =
    InputMapper.subscribeStatic map InputMapped ctx

Then in your program:

open Mibo.Elmish

Program.mkProgram init update
|> Program.withInput
|> Program.withSubscription subscribe

Each frame, the mapper dispatches InputMapped with the current action state:

let update msg model =
    match msg with
    | InputMapped actions ->
        if actions.Started.Contains Jump then
            // do jump
            ()
        struct ({ model with Actions = actions }, Cmd.none)

For a zero-subscription alternative, use Program.withInputMapper which registers an IInputMapper<'Action> service you can query inline.

ActionState gives you three sets each frame:

Field

Description

Held

Actions whose keys are currently down

Started

Actions pressed this frame

Released

Actions released this frame

See Also

type Action = | MoveLeft | MoveRight | Jump | Fire
val map: obj
union case Action.MoveLeft: Action
union case Action.Jump: Action
type Msg = | InputMapped of obj
val subscribe: ctx: 'a -> model: 'b -> 'c
val ctx: 'a
val model: 'b
union case Msg.InputMapped: obj -> Msg
val update: msg: Msg -> model: 'a -> struct ('a * 'b)
val msg: Msg
val model: 'a
val actions: obj

Type something to start searching.