Get updates via , Github, or RSS | About

Episode 32: Keyboard Events Apr 27, 2017

Elm’s Html.Events.onInput lets you handle changes to a textarea, but only if you care about the raw text. If you want to handle keyboard shortcuts, or treating key presses differently when the shift key is held, you need to listen for specific keyboard events.

Examples

View.elm

view =
  textarea
      [ on "keyup" keyCodeAndShiftDecoder
      , placeholder "Your message…"
      , value model.message
      , onInput MessageUpdate
      ]
      []

keyCodeAndShiftDecoder : Decoder Msg
keyCodeAndShiftDecoder =
    Json.Decode.map2 KeyUp
        keyCode
        (Json.Decode.at [ "shiftKey" ] Json.Decode.bool)