我是函数式编程和Purescript的新手。我试图从键盘按下的键获取键码。我创建了一个eventListener,它在触发keydown事件时触发并触发事件监听器函数测试。我在将事件转换为keyboardevent并从keyboardevent获取keycode时遇到问题。我附加了我的代码和错误产生。
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class
import Control.Monad.Eff.Console (CONSOLE, log)
import DOM (DOM)
import DOM.Event.EventTarget (addEventListener, eventListener)
import DOM.HTML.Types as DHT
import DOM.Event.KeyboardEvent as KE
import DOM.Event.Types (EventType(..), EventTarget)
import DOM.Event.Event
import DOM.HTML (window)
import DOM.HTML.Window (document)
import Prelude (Unit)
test :: forall e. Event -> Eff ( console :: CONSOLE, dom :: DOM | e) Unit
test a = do
ke <- KE.eventToKeyboardEvent a
co <- KE.key ke
log "Key Pressed : "
main :: forall e. Eff (console :: CONSOLE, dom :: DOM | e) Unit
main = do
documenttarget <- liftEff $ window >>= document <#> DHT.htmlDocumentToEventTarget
addEventListener (EventType "keydown") (eventListener test) true (documenttarget)
错误:
Error found:
in module Main
at src/Main.purs line 30, column 10 - line 30, column 19
Could not match type
String
with type
t0 t1
while checking that type String
is at least as general as type t0 t1
while checking that expression key ke
has type t0 t1
in value declaration test
where t0 is an unknown type
t1 is an unknown type
See https://github.com/purescript/documentation/blob/master/errors/TypesDoNotUnify.md for more information,
or to contribute content related to this error.
* ERROR: Subcommand terminated with exit code 1
答案 0 :(得分:3)
您的test
功能需要调整一下:
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Except (runExcept)
import DOM (DOM)
import DOM.Event.Event (Event)
import DOM.Event.EventTarget (addEventListener, eventListener)
import DOM.Event.KeyboardEvent as KE
import DOM.Event.Types (EventType(..))
import DOM.HTML (window)
import DOM.HTML.Types as DHT
import DOM.HTML.Window (document)
import Data.Either (Either(..))
test :: forall e. Event -> Eff ( console :: CONSOLE, dom :: DOM | e) Unit
test a =
case runExcept (KE.eventToKeyboardEvent a) of
Left err ->
log "Event was not a keyboard event"
Right ke -> do
let co = KE.key ke
log "Key Pressed : "
main :: forall e. Eff (console :: CONSOLE, dom :: DOM | e) Unit
main = do
documenttarget <- liftEff $ window >>= document <#> DHT.htmlDocumentToEventTarget
addEventListener (EventType "keydown") (eventListener test) true (documenttarget)
首先,KE.key
和KE.eventToKeyboardEvent
不是有效的函数,这意味着您不需要绑定来从中获取值(这是t0 t1
错误的含义:对于某些m String
,它希望结果值与String
相似,而不仅仅是m
。
接下来,当我们使用eventToKeyboardEvent
时,它可能会失败,因为我们无法保证将Event
强制转换为KeyboardEvent
是安全的。结果包含在F
, which is a synonym for Except MultipleErrors
中,因此我们需要做一些事情来获得结果。我们“运行”Except
包装,它会向我们提供Either
,其中Left
方是错误消息,Right
是成功结果。
在这个例子中,我在演员表失败时将其设为日志,但你可能并不关心这一点,并且通常甚至不希望该案例被击中,在这种情况下使用pure unit
可能是好的反而足够了。
如果您愿意让失败案例无效,那么实际上还有另一种写法:
import Data.Foldable (for_)
test :: forall e. Event -> Eff ( console :: CONSOLE, dom :: DOM | e) Unit
test a =
for_ (runExcept (KE.eventToKeyboardEvent a)) \ke -> do
let co = KE.key ke
log "Key Pressed : "
for_
实际上是一个非常通用的函数,可以用于各种各样的东西,但是在对丑陋的DOM API编写代码时,使用它来静默地吸收失败非常方便。