JohnBugner / elm-keyboard / Key

Represents a physical key. Note that this is not the same as which character is produced by a key (or combination of keys). This is a very important distinction to make to ensure that controls still work as intended no matter what keyboard layout the user is using.

For example, in a game where the player moves the character around with the W, A, S, and D keys, you'd want the controls to work no matter what keyboard layout the player is using. On a qwerty keyboard, these keys produce the characters 'w', 'a', 's', and 'd', but on a dvorak keyboard, these keys produce the characters ',', 'a', 'o', and 'e'. Therefore, binding the controls to the characters produced by the keys instead of the physical keys themselves would make the controls effectively unusable for a player using a dvorak keyboard (or any other keyboard, for that matter).

Build


type Key
    = Escape
    | F1
    | F2
    | F3
    | F4
    | F5
    | F6
    | F7
    | F8
    | F9
    | F10
    | F11
    | F12
    | Backquote
    | Digit1
    | Digit2
    | Digit3
    | Digit4
    | Digit5
    | Digit6
    | Digit7
    | Digit8
    | Digit9
    | Digit0
    | Minus
    | Equal
    | Backspace
    | Tab
    | Q
    | W
    | E
    | R
    | T
    | Y
    | U
    | I
    | O
    | P
    | BracketLeft
    | BracketRight
    | Backslash
    | CapsLock
    | A
    | S
    | D
    | F
    | G
    | H
    | J
    | K
    | L
    | Semicolon
    | Quote
    | Enter
    | Shift Side
    | IntlBackslash
    | Z
    | X
    | C
    | V
    | B
    | N
    | M
    | Comma
    | Period
    | Slash
    | Control Side
    | Meta Side
    | Alt Side
    | Space
    | ContextMenu
    | PrintScreen
    | ScrollLock
    | Pause
    | Insert
    | Home
    | PageUp
    | Delete
    | End
    | PageDown
    | ArrowUp
    | ArrowLeft
    | ArrowDown
    | ArrowRight
    | NumLock
    | NumpadDivide
    | NumpadMultiply
    | NumpadSubtract
    | Numpad7
    | Numpad8
    | Numpad9
    | NumpadAdd
    | Numpad4
    | Numpad5
    | Numpad6
    | Numpad1
    | Numpad2
    | Numpad3
    | NumpadEnter
    | Numpad0
    | NumpadDecimal
    | Other String

Represents a key.

Note that keys that don't have a dedicated constructor can still be used by using the Other constructor, as long as you know what string event.code produces.

fromString : String -> Key

Converts a string to a key.

Decode

decoder : Json.Decode.Decoder Key

Decodes which key triggered an event.