From 69d4dcfd9668ed8654207e6614b7d6d73281c9ea Mon Sep 17 00:00:00 2001 From: julien Lengrand-Lambert Date: Fri, 19 Oct 2018 10:14:36 +0200 Subject: [PATCH] Add subscription information to keyboard example The `decoding for games` example is great but I still had some trouble tying everything together when upgrading my game. This adds a short snippet to show how the `subscription` part of the code would look like. --- notes/keyboard.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/notes/keyboard.md b/notes/keyboard.md index ed6a6e3..0587f5d 100644 --- a/notes/keyboard.md +++ b/notes/keyboard.md @@ -87,4 +87,18 @@ toDirection string = By converting to a specialized `Direction` type, the compiler can guarantee that you never forget to handle one of the valid inputs. If it was a `String`, new code could have typos or missing branches that would be hard to find. +Based on this snippet, this is how the `subscription` and `msg` parts of the code could look like : + +```elm +type Msg + = KeyPressed Direction + +... + +subscriptions : Model -> Sub Msg +subscriptions _ = + onKeyDown (Json.Decode.map KeyPressed keyDecoder) +``` + + Hope that helps you write a decoder that works for your scenario!