ohanhi/keyboard-extra/3.0.4

NOTE: this package is not compatible with Elm 0.19.1

Keyboard.Extra

Convenience helpers for working with keyboard inputs.

Msg and Update

Using Keyboard.Extra this way, you get all the help it can provide. Use either this approach, or the plain subscriptions and handle the state yourself.

type Msg

Keyboard.Extra's internal message type.

subscriptions : Sub Msg

The subscriptions needed for the "Msg and Update" way.

update : Msg -> List Key -> List Key

Use this (or updateWithKeyChange) to have the list of keys update.

If you need to know exactly what changed just now, have a look at updateWithKeyChange.

updateWithKeyChange : Msg -> List Key -> ( List Key, Maybe KeyChange )

This alternate update function answers the question: "Did the pressed down keys in fact change just now?"

You might be wondering why this is a Maybe KeyChange – it's because keydown events happen many times per second when you hold down a key. Thus, not all incoming messages actually cause a change in the model.

Note This is provided for convenience, and may not perform well in real programs. If you are experiencing slowness or jittering when using updateWithKeyChange, see if the regular update makes it go away.

= KeyDown Key
| KeyUp Key

The second value updateWithKeyChange may return, representing the actual change that happened during the update.

Helpers

Note: To find out if a key is being pressed, simply use List.member key keyList.

type alias Arrows =
{ x : Int
, y : Int
}

Record type used for arrows and wasd. Both x and y can range from -1 to 1, and are 0 if no keys are pressed.

arrows : List Key -> Arrows

Gives the arrow keys' pressed down state as follows:

arrows []                      --> { x = 0, y = 0 }


arrows [ ArrowLeft ]           --> { x = -1, y = 0 }


arrows [ ArrowUp, ArrowRight ] --> { x = 1, y = 1 }


arrows [ ArrowDown, ArrowLeft, ArrowRight ]
                               --> { x = 0, y = -1 }
wasd : List Key -> Arrows

Similar to arrows, gives the W, A, S and D keys' pressed down state.

wasd []                       --> { x = 0, y = 0 }


wasd [ CharA ]                --> { x = -1, y = 0 }


wasd [ CharW, CharD ]         --> { x = 1, y = 1 }


wasd [ CharA, CharS, CharD ]  --> { x = 0, y = -1 }
= North
| NorthEast
| East
| SouthEast
| South
| SouthWest
| West
| NorthWest
| NoDirection

Type representation of the arrows.

arrowsDirection : List Key -> Direction

Gives the arrow keys' pressed down state as follows:

arrowsDirection []                      --> NoDirection


arrowsDirection [ ArrowLeft ]           --> West


arrowsDirection [ ArrowUp, ArrowRight ] --> NorthEast


arrowsDirection [ ArrowDown, ArrowLeft, ArrowRight ]
                                        --> South
wasdDirection : List Key -> Direction

Similar to arrows, gives the W, A, S and D keys' pressed down state.

wasdDirection []                      --> NoDirection


wasdDirection [ CharA ]               --> West


wasdDirection [ CharW, CharD ]        --> NorthEast


wasdDirection [ CharA, CharS, CharD ] --> South

Plain Subscriptions

If you prefer to only get "the facts" and do your own handling, use these subscriptions. Otherwise, you may be more comfortable with the Msg and Update.

downs : (Key -> msg) -> Sub msg

Subscription for key down events.

Note When the user presses and holds a key, there may or may not be many of these messages before the corresponding key up message.

ups : (Key -> msg) -> Sub msg

Subscription for key up events.

Decoder

targetKey : Decoder Key

A Json.Decoder for grabbing event.keyCode and turning it into a Key

import Json.Decode as Json


onKey : (Key -> msg) -> Attribute msg
onKey tagger =
    on "keydown" (Json.map tagger targetKey)

Low level

fromCode : KeyCode -> Key

Convert a key code into a Key.

fromCode 13  --> Enter
toCode : Key -> KeyCode

Convert a Key into a key code.

toCode Enter  --> 13

Keyboard keys

type Key
= Cancel
| Help
| BackSpace
| Tab
| Clear
| Enter
| Shift
| Control
| Alt
| Pause
| CapsLock
| Escape
| Convert
| NonConvert
| Accept
| ModeChange
| Space
| PageUp
| PageDown
| End
| Home
| ArrowLeft
| ArrowUp
| ArrowRight
| ArrowDown
| Select
| Print
| Execute
| PrintScreen
| Insert
| Delete
| Number0
| Number1
| Number2
| Number3
| Number4
| Number5
| Number6
| Number7
| Number8
| Number9
| Colon
| Semicolon
| LessThan
| Equals
| GreaterThan
| QuestionMark
| At
| CharA
| CharB
| CharC
| CharD
| CharE
| CharF
| CharG
| CharH
| CharI
| CharJ
| CharK
| CharL
| CharM
| CharN
| CharO
| CharP
| CharQ
| CharR
| CharS
| CharT
| CharU
| CharV
| CharW
| CharX
| CharY
| CharZ
| Super
| ContextMenu
| Sleep
| Numpad0
| Numpad1
| Numpad2
| Numpad3
| Numpad4
| Numpad5
| Numpad6
| Numpad7
| Numpad8
| Numpad9
| Multiply
| Add
| Separator
| Subtract
| Decimal
| Divide
| F1
| F2
| F3
| F4
| F5
| F6
| F7
| F8
| F9
| F10
| F11
| F12
| F13
| F14
| F15
| F16
| F17
| F18
| F19
| F20
| F21
| F22
| F23
| F24
| NumLock
| ScrollLock
| Circumflex
| Exclamation
| DoubleQuote
| Hash
| Dollar
| Percent
| Ampersand
| Underscore
| OpenParen
| CloseParen
| Asterisk
| Plus
| Pipe
| HyphenMinus
| OpenCurlyBracket
| CloseCurlyBracket
| Tilde
| VolumeMute
| VolumeDown
| VolumeUp
| Comma
| Minus
| Period
| Slash
| BackQuote
| OpenBracket
| BackSlash
| CloseBracket
| Quote
| Meta
| Altgr
| Other

These are all the keys that have names in Keyboard.Extra.