单击 "Enter" 按键上的按钮

Click button on "Enter" keypress

我想要这样的东西:

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

也就是说,有没有像我刚刚插入的 trigger 函数一样的东西?

为了让 Enter 按键操作像按钮点击一样处理,您不需要按字面意思触发点击。相反,您需要一个在按键或点击发生时触发的事件。最简单的方法是通过 unionWith,特别是如果您已经在使用 Threepenny 的 FRP 组合器(我衷心推荐)。使用那个和其他一些 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,然后使用触发函数触发 [=18] 中的事件=] 点击和按键的处理程序:

-- 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 组合器。