-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMain.elm
70 lines (48 loc) · 1.35 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module Main exposing (Model, Msg(..), init, main, subscriptions, update, view)
import Browser
import Browser.Events as Events
import Debug
import Html exposing (Html, text)
import Json.Decode as Decode
main : Program () Model Msg
main =
Browser.element
{ view = view
, init = \() -> init
, subscriptions = subscriptions
, update = update
}
-- See https://github.com/elm/browser/blob/1.0.0/notes/keyboard.md
type alias Model =
List String
type Msg
= KeyDowns String
| ClearPressed
init : ( Model, Cmd Msg )
init =
( [], Cmd.none )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
KeyDowns code ->
( if List.member code model then
model
else
code :: model
, Cmd.none
)
-- Flush the whole model on `keyup`, helps to remove not pressed keys, if focus was lost from the window.
ClearPressed ->
( [], Cmd.none )
view : Model -> Html Msg
view model =
text (Debug.toString model)
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Events.onKeyDown (Decode.map KeyDowns keyDecoder)
, Events.onKeyUp (Decode.succeed ClearPressed)
]
keyDecoder : Decode.Decoder String
keyDecoder =
Decode.field "key" Decode.string