我正在嘗試通過按Enter鍵提交其輸入的文本字段並通過Escape清除。鍵盤信號和在Elm摺疊
import Graphics.Element exposing (flow, down, show)
import Signal exposing ((<~), (~))
import Graphics.Input.Field exposing (noContent, defaultStyle, field)
import String
import Keyboard
main = Signal.map view (Signal.foldp step init signals)
signals = (,,) <~ box.signal
~ Signal.sampleOn Keyboard.enter box.signal
~ escape
escape = Keyboard.isDown 27
box = Signal.mailbox noContent
init = (noContent, noContent, False)
view (text, query, clear) =
flow down
[ field defaultStyle (Signal.message box.address) "Enter text" text
, show (String.words query.string)
]
step (text, query, clear) _ =
case clear of
False -> (text, query, clear)
True -> (noContent, noContent, clear)
僅當按住Escape鍵時纔會產生一個空字段,並且會在釋放Escape時恢復爲輸入的內容。
試圖理解爲什麼是這樣的話使我一個小例子:
import Graphics.Element exposing (show)
import Signal
import Char
import String
import Keyboard
main = Signal.map show input
input = Signal.foldp step "" (Signal.map2 (,) Keyboard.presses escapeDown)
escapeDown = Keyboard.isDown 27
step (keyCode, esc) string =
case esc of
True -> ""
False -> string ++ keyToString keyCode
keyToString = String.fromChar << Char.fromCode
字符的累積串被清空上按下Esc鍵,但其釋放導致單一的字符串(最後進入)字符。
據我所知,Keyboard.isDown
信號在按住鍵和釋放鍵時被觸發。那麼我怎樣才能持久地清理這個領域呢?