KeyboardEvent.location returns 没有

KeyboardEvent.location returns nothing

我有一段代码:

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

看起来 e.location returns 什么都没有,"undefined" 出现在控制台。 基本任务是检测何时按下左移,何时按下右移。 我去了 Google 并找到了 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键,因为左shift的origanalEvent.location1而右shift的origanalEvent.location2并且它似乎任何其他键的 origanalEvent.location0