gpt4 book ai didi

javascript - KeyboardEvent.location 不返回任何内容

转载 作者:行者123 更新时间:2023-12-02 15:21:27 24 4
gpt4 key购买 nike

我有一段代码:

$('body').on('keydown', function(e) {
console.log(e.location)
});

看起来 e.location 没有返回任何内容,控制台上出现“未定义”。基本任务是检测何时按下左移以及何时按下右移。我去谷歌发现this ,但这对我不起作用。有什么建议吗?

最佳答案

我相信你想要的是e.originalEvent.location - 我在e对象周围做了一些窥探,发现了这个:

右移:

altKey: false
bubbles: true
cancelable: true
char: undefined
charCode: 0
ctrlKey: false
currentTarget: body
data: undefined
delegateTarget: body
eventPhase: 2
handleObj: Object
isDefaultPrevented: returnFalse()
jQuery21406245628797914833: true
key: undefined
keyCode: 16
metaKey: false
originalEvent: KeyboardEvent
altKey: false
bubbles: true
cancelBubble: false
cancelable: true
charCode: 0
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
isTrusted: true
isTrusted: true
keyCode: 16
keyIdentifier: "Shift"
keyLocation: 2
location: 2 // <---------------------

左移

altKey: false
bubbles: true
cancelable: true
char: undefined
charCode: 0
ctrlKey: false
currentTarget: body
data: undefined
delegateTarget: body
eventPhase: 2
handleObj: Object
isDefaultPrevented: returnFalse()
jQuery21406245628797914833: true
key: undefined
keyCode: 16
metaKey: false
originalEvent: KeyboardEvent
altKey: false
bubbles: true
cancelBubble: false
cancelable: true
charCode: 0
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
isTrusted: true
isTrusted: true
keyCode: 16
keyIdentifier: "Shift"
keyLocation: 2
location: 1 // <---------------------

使用此功能,您可以知道按下了哪个 Shift 键,因为左移的 originalEvent.location1,右移的 originalEvent.location/code> 为 2,似乎任何其他键的 originalEvent.location0

关于javascript - KeyboardEvent.location 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com