bburdette / windowkeys / WindowKeys

This WindowKeys Elm module lets you encode and decode messages to pass to javascript, where the actual key event listening will take place. See the README for more.


type alias Key =
{ key : String
, ctrl : Basics.Bool
, alt : Basics.Bool
, shift : Basics.Bool
, preventDefault : Basics.Bool 
}

Key struct - both outgoing "SetWindowKeys" and incoming keypress messages.


type WindowKeyCmd
    = SetWindowKeys (List Key)

Only one WindowKeyCmd for now, SetWindowKeys. Use an empty list to stop all key messages.

decodeKey : Json.Decode.Decoder Key

json to Key struct

encodeKey : Key -> Json.Encode.Value

Key struct to json

receive : (Result Json.Decode.Error WindowKeyMsg -> msg) -> Json.Decode.Value -> msg

make a subscription function with receive and a port, like so:

port receiveKeyMsg : (JD.Value -> msg) -> Sub msg
keyreceive =
    receiveKeyMsg <| WindowKey.receive WsMsg

Where WkMsg is defined in your app like this:

type Msg
    = WkMsg (Result JD.Error WindowKey.WindowKeyMsg)
    | <other message types>

then in your application subscriptions:

subscriptions =
   \_ -> keyreceive

send : (Json.Encode.Value -> Platform.Cmd.Cmd msg) -> WindowKeyCmd -> Platform.Cmd.Cmd msg

use send to make a convenience function, like so:

port sendKeyCommand : JE.Value -> Cmd msg
wksend =
   WindowKey.send sendKeyCommand

then you can call (makes a Cmd):

wksend <|
    SetWindowKeys
        [ { key = "s"
          , ctrl = True
          , alt = False
          , shift = False
          , preventDefault = True }
        , { key = "Enter"
          , ctrl = False
          , alt = False
          , shift = False
          , preventDefault = False }
        ]