ohanhi/keyboard/1.0.0

NOTE: the latest version is 2.0.1

Keyboard

Convenience helpers for working with keyboard inputs.

Msg and Update

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.

type Msg

Keyboard's internal message type.

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

update : Msg -> List Key -> List Key

Use this to have the list of keys update.

This will give you all the keys I can recognize.

  • If you encounter sluggish performance and need to optimize your program, try updateWithParser.
  • If you need to know exactly what changed just now, have a look at 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 = [] }
    ...

More advanced

updateWithParser : KeyParser -> Msg -> List Key -> List Key

A more advanced version of update. Provide it with a smaller KeyParser than anyKey and it will perform a little bit faster.

= KeyDown Key
| KeyUp Key

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

updateWithKeyChange : KeyParser -> 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. Also, you will only get updates for the keys that match your KeyParser.

Key parsers

type RawKey

An unprocessed key value.

Use a KeyParser to turn it into something useful.

type alias KeyParser =
RawKey -> Maybe Key

A key parser can turn RawKeys into meaningful Keys for your program.

anyKey : KeyParser

This parser tries to match with all the keys I can recognize. It is used in update.

This might be slow! If you only need e.g. arrow keys, you can use navigationKey instead.

If the key doesn't match any of the categories, Nothing is returned.

Returns the character that was pressed.

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 modifier keys.

[Alt] -> Just Alt

[Tab] -> Nothing

Converts a RawKey if it is one of the whitespace keys.

[Tab] -> Just Tab

[Alt] -> Nothing

editingKey : RawKey -> Maybe Key

Converts a RawKey if it is one of the editing keys.

[Backspace] -> Just Backspace

[Enter] -> Nothing

Converts a RawKey if it is one of the function keys.

[F4] -> Just F4

[6] -> Nothing

phoneKey : RawKey -> Maybe Key

Converts a RawKey if it is one of the phone keys.

mediaKey : RawKey -> Maybe Key

Converts a RawKey if it is one of the media keys.

Combining key parsers

oneOf : List KeyParser -> RawKey -> Maybe Key

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.

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 : (RawKey -> 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 : (RawKey -> msg) -> Sub msg

Subscription for key up events.

Low level

rawValue : RawKey -> String

Get the original string value of the RawKey.

Keyboard keys

type Key
= Character String
| Alt
| AltGraph
| CapsLock
| Control
| Fn
| FnLock
| Hyper
| Meta
| NumLock
| ScrollLock
| Shift
| Super
| Symbol
| SymbolLock
| Enter
| Tab
| Spacebar
| ArrowDown
| ArrowLeft
| ArrowRight
| ArrowUp
| End
| Home
| PageDown
| PageUp
| Backspace
| Clear
| Copy
| CrSel
| Cut
| Delete
| EraseEof
| ExSel
| Insert
| Paste
| Redo
| Undo
| F1
| F2
| F3
| F4
| F5
| F6
| F7
| F8
| F9
| F10
| F11
| F12
| F13
| F14
| F15
| F16
| F17
| F18
| F19
| F20
| Again
| Attn
| Cancel
| ContextMenu
| Escape
| Execute
| Find
| Finish
| Help
| Pause
| Play
| Props
| Select
| ZoomIn
| ZoomOut
| AppSwitch
| Call
| Camera
| CameraFocus
| EndCall
| GoBack
| GoHome
| HeadsetHook
| LastNumberRedial
| Notification
| MannerMode
| VoiceDial
| ChannelDown
| ChannelUp
| MediaFastForward
| MediaPause
| MediaPlay
| MediaPlayPause
| MediaRecord
| MediaRewind
| MediaStop
| MediaTrackNext
| MediaTrackPrevious

These are all the keys that have names in Keyboard.