halfzebra / elm-examples

:book: Practical examples in Elm
210 stars 22 forks source link

Improve key-combinators example #4

Closed welf closed 7 years ago

welf commented 8 years ago
  1. When you press Tab key, the focus goes to the address bar of browser and the KeyCode of this key remains in the model, because Elm app doesn't get the event Keyboard.ups. It is better to clear the model explicitly on Keyboard.ups (at least the model will be cleared on the next Keyboard.ups event).
  2. It is better to use :: operator instead of ++.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        KeyDowns code ->
            ( if code `List.member` model then
                model
              else
                code :: model
            , Cmd.none
            )

        KeyUps code ->
            ( [], Cmd.none )
halfzebra commented 7 years ago

@welf thank you for your contribution! I have improved the example in #5

I haven't solved the problem with Tab, the only reasonable way is to clear up the list of pressed buttons right away, but then it would make sense to handle other combinations, which will make browser window to loose focus.

I'm not sure if this would be an appropriate place for a comprehensive example.

You are very welcome to hit me with a PR, if you have good ideas on that.

For now I will close this issue.