2015-12-03 90 views
4

按钮,我想是这样的:点击“回车”按键

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

也就是说,是有什么,它如同trigger功能我刚插入?

回答

1

为了让输入按键处理就好像它们是按钮点击一样,您不需要直接触发点击。相反,您需要一个无论何时发生按键或点击都会触发的事件。最简单的方法是,特别是如果您已经使用Threepenny的玻璃钢组合器(我衷心推荐),通过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 

然后你只需处理eGo,使用onEvent,在你处理click事件的方式。

注意的是,在你的问题的伪精神的另一种解决办法是通过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组合器。

+0

如何为eGo添加2个以上的活动? 你能举一个newEvent构造的例子吗?谢谢! – allidoiswin

+1

@allidoiswin(1)多次使用'unionWith'(例如'unionWith const eA(unionWith const eB eC)')或['union'](https://hackage.haskell.org/package/threepenny-gui-0.6 .0.3/docs/Reactive-Threepenny.html#v:union)(例如'void(union [eA,eB,eC])')。 (2)我增加了一个替代方案的例子。 – duplode