您需要使用常规StartApp
而不是StartApp.Simple
因为它提供了使用效果和任务的方法.
当按键不是空格键时,Action
需要一个NoOp
构造函数来允许我们的视图保持不变.
然后你需要一个将Keyboard.presses
值映射到a的函数Action
.这是更新的代码:
import Html exposing (div, button, text)
import Html.Events exposing (onClick)
import StartApp
import Effects exposing (Never)
import Task
import Keyboard
import Char
app =
StartApp.start
{ init = init
, view = view
, update = update
, inputs = [ Signal.map spaceToInc Keyboard.presses ]
}
main =
app.html
type alias Model = Int
init =
(0, Effects.none)
view address model =
div []
[ button [ onClick address Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick address Increment ] [ text "+" ]
]
type Action
= Increment
| Decrement
| NoOp
update action model =
case action of
NoOp -> (model, Effects.none)
Increment -> (model + 1, Effects.none)
Decrement -> (model - 1, Effects.none)
spaceToInc : Int -> Action
spaceToInc keyCode =
case (Char.fromCode keyCode) of
' ' -> Increment
_ -> NoOp
port tasks : Signal (Task.Task Never ())
port tasks =
app.tasks