点击“Enter”按键上的按钮

时间:2015-12-03 02:33:55

标签: haskell threepenny-gui

我想要这样的事情:

on UI.keydown textfield $ \c -> when (c == 13) $ void $ do
    trigger UI.click button

即,是否存在类似我刚刚插入的trigger函数的内容?

1 个答案:

答案 0 :(得分:1)

为了让 Enter 按键处理,就好像它们是按钮点击一样,您不需要按字面意思触发点击。相反,您需要在按键或单击发生时触发的事件。最简单的方法,特别是如果你已经在使用Threepenny的FRP组合器(我衷心推荐),可以通过unionWith。使用它和其他一些Reactive.Threepenny组合器,代码可能如下所示:

-- I'm giving separate names to the events for extra clarity.
let eClick = UI.click button
    eEnter = void $ filterE (== 13) (UI.keydown textfield)
    -- This event is fired whenever `eClick` or `eEnter` happen.
    eGo = unionWith const eClick eEnter

然后,您只需使用onEvent处理eGo,就像处理点击事件一样。

请注意,您问题中伪代码精神的替代解决方案是通过newEvent定义eGo,然后使用触发器函数触发on中的事件点击和按键的处理程序:

-- Assuming you are in an `UI` do-block, thus the `liftIO`.
(eGo, fireGo) <- liftIO newEvent
on UI.click button $ \_ -> fireGo ()
on UI.keydown textfield $ \c -> when (c == 13) (fireGo ())
-- Alternatively, you might define an `eEnter` with `filterE`,
-- as in the first example, instead of using `on` and `when`. 

这不像第一个解决方案那么好,因为它有点混乱,并且与FRP代码的播放效果不是很好。我不推荐它,除非您根本不使用FRP组合器。