如何检测Elm中的shift-enter?

时间:2016-07-17 06:35:27

标签: elm

我已经调整了todomvc example中的 Software Configuration: spring-core 4.1.6.RELEASE, spring-data-mongodb 1.8.2.RELEASE, mongo-java-driver 3.1.0, spring-context 4.1.6.RELEASE, Tomcat 8. 代码来创建 2016-07-16T12:29:12.835+0530 I CONTROL [initandlisten] MongoDB starting : pid=792 port=27017 dbpath=C:\mongodata 64-bit host=DESKTOP-SFNL93Q 2016-07-16T12:29:12.956+0530 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 2016-07-16T12:29:12.960+0530 I CONTROL [initandlisten] db version v3.2.6 2016-07-16T12:29:12.961+0530 I CONTROL [initandlisten] git version: 05552b562c7a0b3143a729aaa0838e558dc49b25 2016-07-16T12:29:12.962+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p-fips 9 Jul 2015 2016-07-16T12:29:12.964+0530 I CONTROL [initandlisten] allocator: tcmalloc 2016-07-16T12:29:12.965+0530 I CONTROL [initandlisten] modules: none 2016-07-16T12:29:12.966+0530 I CONTROL [initandlisten] build environment: 2016-07-16T12:29:12.967+0530 I CONTROL [initandlisten] distmod: 2008plus-ssl 2016-07-16T12:29:12.968+0530 I CONTROL [initandlisten] distarch: x86_64 2016-07-16T12:29:12.969+0530 I CONTROL [initandlisten] target_arch: x86_64 2016-07-16T12:29:12.970+0530 I CONTROL [initandlisten] options: { storage: { dbPath: "C:\mongodata" } } 2016-07-16T12:29:12.993+0530 I - [initandlisten] Detected data files in C:\mongodata created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 2016-07-16T12:29:12.999+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2016-07-16T12:29:14.717+0530 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker 2016-07-16T12:29:14.717+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/mongodata/diagnostic.data' 2016-07-16T12:29:14.744+0530 I NETWORK [initandlisten] waiting for connections on port 27017 2016-07-16T12:33:51.795+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53065 #1 (1 connection now open) 2016-07-16T12:41:16.247+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53243 #2 (2 connections now open) 2016-07-16T12:41:58.268+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53253 #3 (3 connections now open) 2016-07-16T12:41:58.318+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53254 #4 (4 connections now open) 2016-07-16T12:42:02.864+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53255 #5 (5 connections now open) 2016-07-16T12:42:02.922+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53256 #6 (6 connections now open) 2016-07-16T12:42:04.920+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53257 #7 (7 connections now open) 2016-07-16T12:42:04.958+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53258 #8 (8 connections now open) 2016-07-16T12:46:51.214+0530 I NETWORK [conn1] end connection 127.0.0.1:53065 (7 connections now open) 2016-07-16T12:46:51.216+0530 I NETWORK [conn2] end connection 127.0.0.1:53243 (7 connections now open) 2016-07-16T12:48:51.768+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53378 #9 (7 connections now open) 2016-07-16T12:51:07.622+0530 I NETWORK [conn9] end connection 127.0.0.1:53378 (6 connections now open) 2016-07-16T12:53:27.581+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53403 #10 (7 connections now open) 2016-07-16T12:54:20.400+0530 I NETWORK [conn3] end connection 127.0.0.1:53253 (6 connections now open) 2016-07-16T12:54:20.401+0530 I NETWORK [conn4] end connection 127.0.0.1:53254 (6 connections now open) 2016-07-16T12:55:38.285+0530 I NETWORK [conn10] end connection 127.0.0.1:53403 (4 connections now open) ,但它不起作用。显然,onenter没有传递给榆树。那么,我怎样才能检测到shift-Enter?

onShiftEnter

3 个答案:

答案 0 :(得分:3)

使用Json.Decoder.object2代替。 Json.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

答案 1 :(得分:1)

根据https://developer.mozilla.org/en-US/docs/Web/Events/keydown判断,你需要'Key',而不是'Code',即

(Json.Decode.at ["shiftKey"] Json.Decode.bool)

答案 2 :(得分:0)

自@Tosh回答以来,Elm发生了变化。 tuple2object2都不作为标准解码功能。 :=(,)都不可用。现在看起来像:

import Json.Decode as Json

onShiftEnter : Msg -> Attribute Msg
onShiftEnter msg =
    let
        tagger ( code, shift ) =
            if code == 13 && shift then
                msg

            else
                NoOp

        keyExtractor =
            Json.map2 Tuple.pair
                (Json.field "keyCode" Json.int)
                (Json.field "shiftKey" Json.bool)
    in
    on "keydown" <| Json.map tagger keyExtractor

尽管我个人更喜欢import Json.Decode as Decode作为约定。

我会编辑Tosh的答案,但在撰写本文时是准确的,并且与问题代码的样式和版本相匹配。