ohanhi/keyboard-extra/3.0.0

NOTE: this package is not compatible with Elm 0.19.1

Keyboard.Extra

Convenience helpers for working with keyboard inputs.

Intelligent Helper

Using Keyboard.Extra this way, you get all the help it can provide. You should not use this together with the plain subscriptions.

type Msg

The message type Keyboard.Extra uses.

subscriptions : Sub Msg

You will need to add this to your program's subscriptions.

update : Msg -> List Key -> List Key

You need to call this (or updateWithKeyChange) to have the set of pressed down keys update. If you need to know exactly what changed just now, have a look at the updateWithKeyChange.

= KeyDown Key
| KeyUp Key

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

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.

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:

  • { x = 0, y = 0 } when pressing no arrows.
  • { x =-1, y = 0 } when pressing the left arrow.
  • { x = 1, y = 1 } when pressing the up and right arrows.
  • { x = 0, y =-1 } when pressing the down, left, and right arrows (left and right cancel out).
wasd : List Key -> Arrows

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

  • { x = 0, y = 0 } when pressing none of W, A, S and D.
  • { x =-1, y = 0 } when pressing A.
  • { x = 1, y = 1 } when pressing W and D.
  • { x = 0, y =-1 } when pressing A, S and D (A and D cancel out).
= 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:

  • None when pressing no arrows.
  • West when pressing the left arrow.
  • NorthEast when pressing the up and right arrows.
  • South when pressing the down, left, and right arrows (left and right cancel out).
wasdDirection : List Key -> Direction

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

  • None when pressing none of W, A, S and D.
  • West when pressing A.
  • NorthEast when pressing W and D.
  • South when pressing A, S and D (A and D cancel out).

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 Intelligent Helper.

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.

toCode : Key -> KeyCode

Convert a Key into a key code.

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.