Purescript无法从键盘获取键码

时间:2018-03-03 11:23:02

标签: dom functional-programming addeventlistener 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

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.keyKE.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编写代码时,使用它来静默地吸收失败非常方便。