如何在Elm中检测移位输入?
我已将中的如何在Elm中检测移位输入?,elm,Elm,我已将中的oneter代码改编为创建onShift enter,但它不起作用。显然,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"
oneter
代码改编为创建onShift enter
,但它不起作用。显然,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.Decode.at ["shiftKey"] Json.Decode.bool)
改用
Json.Decoder.object2
。
Json.Decoder.tuple2
用于解码数组
import Json.Decode为Json公开((:=)
onShift输入:消息->属性消息
OnShift输入消息=
让
标记器(代码、班次)=
如果代码==13&&shift,则消息else NoOp
钥匙拔出器=
Json.object2(,)
(“keyCode”:=Json.int)
(“shiftKey”:=Json.bool)
在里面
在“keydown”上,自从@Tosh的回答之后,Elm已经改变了。tuple2
和object2
都不是标准解码函数。:=
和(,)
都不可用。现在看起来更像:
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
导入Json。解码为Json
onShift输入:消息->属性消息
OnShift输入消息=
让
标记器(代码、班次)=
如果代码==13&&shift,则
味精
其他的
努普
钥匙拔出器=
Json.map2 Tuple.pair
(Json.field“keyCode”Json.int)
(Json.field“shiftKey”Json.bool)
在里面
不幸的是,在“keydown”上没有。我现在已经编辑了使用shiftkey的问题:仍然不工作。