elm - 如何检测 Elm 中的 shift-enter?

标签 elm

我已经改编了 todomvc example 中的 onenter 代码创建 onShiftEnter,但它不起作用。显然,shiftKey 没有传递给 Elm。那么,如何检测 shift-Enter ?

onShiftEnter : Msg -> Attribute Msg
onShiftEnter msg =
  let
    tagger (code, shift) =
      if code == 13 && shift then msg else NoOp
  in
    on "keydown" 
       (Json.Decode.map tagger 
          ( Json.Decode.tuple2 (,)
              (Json.Decode.at ["keyCode"] Json.Decode.int)
              (Json.Decode.at ["shiftKey"] Json.Decode.bool)
          )
       )

最佳答案

改用Json.Decoder.object2Json.Decoder.tuple2 用于解码数组。

import Json.Decode as Json exposing ((:=))

onShiftEnter : Msg -> Attribute Msg
onShiftEnter msg =
  let
    tagger (code, shift) =
      if code == 13 && shift then msg else NoOp
    keyExtractor =
      Json.object2 (,)
        ("keyCode" := Json.int)
        ("shiftKey" := Json.bool)
  in
    on "keydown" <| Json.map tagger keyExtractor

关于elm - 如何检测 Elm 中的 shift-enter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38418573/

相关文章:

elm 入门文档 "cannot find module"

elm - 假设 a != b 与 Elm 模糊测试

xml - 榆树:读取文件内容

function - 如何在 Elm repl 中查找函数的类型

elm - 榆树有像镜头一样的haskell或类似的东西吗?

node.js - 有没有办法阻止 gulp-elm 中的 elm.init() 将 elm-stuff 和 elm-package.json 移动到父目录中?

url - 如何从 elm 中的绝对路径创建 url

elm - 尝试在同一节点中的两个嵌入式 Elm 应用程序之间切换失败

types - 理解这个 elm url-parser Parser 类型声明

recursion - 编译器用多态递归函数耗尽内存