This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
README

MultikeyHandling

Extra helpers for building Html.Attribute handling multiple key combinations.

Html Attribute builder

onKeydown : Json.Decoder msg -> Attribute msg

Build Html.Attribute from Json.Decoder

Predefined key decoders

whenKeydown : Int -> msg -> Json.Decoder msg -> Json.Decoder msg

Build Decoder on custom key code

whenEnter : msg -> Json.Decoder msg -> Json.Decoder msg

Build Decoder on enter key

whenMetaEnter : msg -> Json.Decoder msg -> Json.Decoder msg

Build Decoder on Meta + Enter key

whenEscape : msg -> Json.Decoder msg -> Json.Decoder msg

Build Decoder on Escape key

defaultKeyHandler : Json.Decoder msg

Default key handler, always return fails

module MultikeyHandling
  exposing
    ( defaultKeyHandler
    , onKeydown
    , whenKeydown
    , whenEnter
    , whenMetaEnter
    , whenEscape
    )


{-| Extra helpers for building Html.Attribute handling multiple key combinations.

# Html Attribute builder

@docs onKeydown

# Predefined key decoders

@docs whenKeydown, whenEnter, whenMetaEnter, whenEscape, defaultKeyHandler

-}

import Json.Decode as Json
import Html exposing (Attribute)
import Html.Events exposing (..)


{-| Default key handler, always return fails
-}
defaultKeyHandler : Json.Decoder msg
defaultKeyHandler =
  Json.fail "Default handler"


{-| Build Html.Attribute from Json.Decoder
-}
onKeydown : Json.Decoder msg -> Attribute msg
onKeydown decoder =
  on "keydown" decoder


{-| Build Decoder on custom key code
-}
whenKeydown : Int -> msg -> Json.Decoder msg  -> Json.Decoder msg
whenKeydown code msg defaultDecoder =
  keyCode
    |> Json.andThen
        (\c ->
            if c == code
              then Json.succeed msg
              else defaultDecoder
        )

{-| Build Decoder on enter key
-}
whenEnter : msg -> Json.Decoder msg  -> Json.Decoder msg
whenEnter =
  whenKeydown 13


{-| Build Decoder on Meta + Enter key
-}
whenMetaEnter : msg -> Json.Decoder msg  -> Json.Decoder msg
whenMetaEnter msg decoder =
  whenKeydown 13 msg decoder
    |> withMetaKey


{-| Build Decoder on Escape key
-}
whenEscape : msg -> Json.Decoder msg  -> Json.Decoder msg
whenEscape =
  whenKeydown 27


withMetaKey : Json.Decoder msg -> Json.Decoder msg
withMetaKey decoder =
  Json.field "metaKey" Json.bool
    |> Json.andThen (\x -> if x then decoder else Json.fail "not meta")