Purescript无法从键盘获取键码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Purescript无法从键盘获取键码相关的知识,希望对你有一定的参考价值。
我是函数式编程和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
你的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的代码时,使用它来静默地吸收失败非常方便。
以上是关于Purescript无法从键盘获取键码的主要内容,如果未能解决你的问题,请参考以下文章