Higher level handling for keyboard inputs in Elm 0.17.
This library is a standard component following The Elm Architecture.
It is quite tedious to find out the currently pressed down keys with just the Keyboard module, so this package aims to make it easier. You can also get some bonus benefits out of this:
Key type, including e.g. ArrowUp, CharA and EnterCharC is pressed down when any Msg happens in your program{ x : Int, y : Int }There is a full example module in the source repository.
In essence, Keyboard.Extra is just another component in your program. It has a model, an update function and some subscriptions. Below are the necessary parts to wire things up. Once that is done, you can go and use the helper functions such as isPressed and arrows. Have fun! :)
-- include the model in your component
type alias Model =
{ keyboardModel : Keyboard.Extra.Model
, otherThing : Int
-- ...
}-- initialize the component
init : ( Model, Cmd Msg )
init =
let
( keyboardModel, keyboardCmd ) =
Keyboard.Extra.init
in
( { keyboardModel = keyboardModel
, otherThing = 0
-- ...
}
, Cmd.batch
[ Cmd.map KeyboardExtraMsg keyboardCmd
-- ..
]
)-- add the message type in your messages
type Msg
= KeyboardExtraMsg Keyboard.Extra.Msg
| OtherMsg-- let it update its model
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
KeyboardExtraMsg keyMsg ->
let
( keyboardModel, keyboardCmd ) =
Keyboard.Extra.update keyMsg model.keyboardModel
in
( { model | keyboardModel = keyboardModel }
, Cmd.map KeyboardMsg keyboardCmd
)
-- ...-- and finally, include the subscriptions for the events to come through
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Sub.map KeyboardExtraMsg Keyboard.Extra.subscriptions
-- ...
]