3

I need Keyboard keyup signal. But the STD library has only keydown which causes "freezes" in my program thanks to very fast changing game state (pause and play). How to solve it?

4

1 回答 1

5

查看 elm-signal-extra 包:http ://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/3.3.1

具体来说,有一个函数Signal.Discrete.whenChangeTo : a -> Signal a -> EventSourceEventSource是 的类型别名Signal ()

True每次按下 Enter 键时,以下程序将在屏幕上显示 500 毫秒:

import Text (asText)
import Keyboard
import Signal
import Signal.Discrete (whenChangeTo)
import Signal.Time (since)

enterKeyUp = whenChangeTo False (Keyboard.isDown 13)

main = Signal.map asText (since 500 enterKeyUp)

编辑:

我添加了since 500 enterKeyUp一个简单的视觉效果来查看enterKeyUp信号是否正常工作。这是另一个示例,展示了如何在没有 500 ms 部分的情况下使用它。它显示回车键被释放的次数:

import Text (asText)
import Keyboard
import Signal
import Signal.Discrete (whenChangeTo)
import Signal.Time (since)

enterKeyUp = whenChangeTo False (Keyboard.isDown 13)

count : Signal a -> Signal Int
count signal = foldp (\_ x -> x + 1) 0 signal

main = Signal.map asText (count enterKeyUp)
于 2015-03-10T03:03:05.713 回答