Convenience helpers for working with keyboard inputs.
Using Keyboard this way, you get all the help it can provide. Use either this approach, or the plain subscriptions and handle the state yourself.
The subscriptions needed for the "Msg and Update" way.
Use this to have the list of keys update.
This will give you all the keys I can recognize.
updateWithParser.updateWithKeyChange.Note! If the user presses a key combination that shifts focus, such as Alt-Tab or Ctrl-L,
some keys may get "stuck" in the keys list. One solution to this issue is to create a port subscription to the window blur events and clearing the entire key list from your model:
// JavaScript
window.onblur = function() { elmApp.ports.blurs.send({}) }
-- Elm
port blurs : (() -> msg) -> Sub msg
subscriptions : Sub Msg
subscriptions =
Sub.batch
[ Keyboard.subscriptions KeyboardMsg
, blurs Blur
]
-- update
...
Blur ->
{ model | pressedKeys = [] }
...A more advanced version of update. Provide it with a smaller KeyParser than anyKey and
it will perform a little bit faster.
The second value updateWithKeyChange may return, representing the actual
change that happened during the update.
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. Also, you will
only get updates for the keys that match your KeyParser.
An unprocessed key value.
Use a KeyParser to turn it into something useful.
A key parser can turn RawKeys into meaningful Keys for your program.
This parser tries to match with all the keys I can recognize. Spacebar is used for the space
key and Characters are all uppercase. This parser is used in update.
If the key doesn't match any of the categories, Nothing is returned.
Note: If you experience performance issues, you can use oneOf with some specific parsers.
The same as anyKeyUpper, but with Characters in the original case.
Returns the character that was pressed, always uppercase.
NOTE There is no reasonable way of actually telling if a certain key is a character or not. For now at least, consider this a Western language focused "best guess".
Examples on a US layout:
[A] -> Just (Character "A")
[Shift] + [A] -> Just (Character "A")
[Shift] + [1] -> Just (Character "!")
[Shift] -> Nothing
Returns the character that was pressed, in the original case.
NOTE There is no reasonable way of actually telling if a certain key is a character or not. For now at least, consider this a Western language focused "best guess".
Examples on a US layout:
[A] -> Just (Character "a")
[Shift] + [A] -> Just (Character "A")
[Shift] + [1] -> Just (Character "!")
[Shift] -> Nothing
Converts a RawKey if it is one of the phone keys.
Converts a RawKey if it is one of the media keys.
Turn any RawKey into a Key using the processing functions (modifierKey, whitespaceKey,
etc.) provided. If the key doesn't match any of the categories, Nothing is returned.
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.
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.
Use this with Html keyboard events to retrieve a RawKey representing the key
which triggered the event.
Html.Events.on "keydown" eventKeyDecoderThese are all the keys that have names in Keyboard.
Keyboard's internal message type.