Module Brr.Key

User keyboard.

Physical keys

Note. Physical keys are for using the keyboard as a controller. They must not be used to derive text input: they are unrelated to the user's keyboard layout for text entry.

type code = int

The type for physical key codes.

type t = [
| `Alt of [ `Left | `Right ]
| `Arrow of [ `Up | `Down | `Left | `Right ]
| `Ascii of Stdlib.Char.t
| `Backspace
| `Ctrl of [ `Left | `Right ]
| `End
| `Enter
| `Escape
| `Func of int
| `Home
| `Insert
| `Key of code
| `Meta of [ `Left | `Right ]
| `Page of [ `Up | `Down ]
| `Return
| `Shift of [ `Left | `Right ]
| `Spacebar
| `Tab
]

The type for physical keys.

Warning. This type is overdefined for now. For example except for `Meta, `Left and `Right modifiers cannot be distinguished; `Left is always returned. `Enter and `Return cannot be distinguished, `Return is always returned.

val of_ev : Js_of_ocaml.Dom_html.keyboardEvent Ev.t -> t

of_ev e is the physical key of keyboard event e.

val equal : t -> t -> bool

equal k0 k1 is true iff k0 and k1 are equal.

val compare : t -> t -> int

compare is a total order on keys compatible with equal.

val pp : Stdlib.Format.formatter -> t -> unit

pp formats keys.

Keyboard events

type events

The type for gathering keyboard events on a given target.

val for_target : ?⁠capture:bool -> ?⁠propagate:bool -> ?⁠default:bool -> 'a Ev.target -> events

for_target t is keyboard events for target t. The other parameters are those of Ev.for_target.

val for_el : ?⁠capture:bool -> ?⁠propagate:bool -> ?⁠default:bool -> El.t -> events

for_el e is like for_target but for an element.

Key events

val any_down : events -> t Note.event

any_down evs occurs whenever a key goes down on the target.

val any_up : events -> t Note.event

any_down evs occurs whenever a key goes up on the target.

val any_holds : events -> bool Note.signal

any_holds evs is true whenever any key is down.

val down : events -> t -> unit Note.event

down evs k occurs whenever key k goes down on the target.

val up : events -> t -> unit Note.event

up evs k occurs whenever key k goes up on the target.

val holds : events -> t -> bool Note.signal

holds evs k is true whenever k is held down on the target.

Modifiers signals

val alt : events -> bool Note.signal

alt evs is true whenver an alt key is down on the target. Equivalent to:

S.Bool.(holds evs (`Alt `Left) || holds evs (`Alt `Right))
val ctrl : events -> bool Note.signal

ctrl evs is true whenver an ctrl key is down on the target. Equivalent to:

S.Bool.(holds evs (`Ctrl `Left) || holds evs (`Ctrl `Right))
val meta : events -> bool Note.signal

meta evs is true whenver an meta key is down on the target. Equivalent to:

S.Bool.(holds evs (`Meta `Left) || holds evs (`Meta `Right))
val shift : events -> bool Note.signal

shift evs is true whenver an shift key is down on the target. Equivalent to:

S.Bool.(holds evs (`Meta `Left) || holds evs (`Meta `Right))

Semantic incoherences

holds and any_holds may be initially set to false even though they should be true if for_target is invoked when the corresponding keys are depressed.

Key repeat events

Key repeat events are not exposed. There are two main use cases for key repeat. First during text input, but his should be handled by text input events and is out of scope. Second for controlling changes to a variable over time, e.g. scrolling with a keyboard. In the latter case it is better to create a timing signal or event with a known rate while the key is held.