单击"Enter"按键上的按钮



我想要这样的东西:

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,然后使用触发器函数在点击和按键的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组合子。

最新更新