KeyboardEvent.location什么都不返回

时间:2015-11-30 23:19:07

标签: javascript jquery javascript-events keyboard-events

我有一段代码:

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

看起来e.location没有返回任何内容,“undefined”出现在控制台上。 基本任务是检测何时按下左移和何时正确。 我去谷歌找到了this,但它对我不起作用。 有什么建议吗?

1 个答案:

答案 0 :(得分:2)

我相信你想要的是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键,因为左移的origanalEvent.location1而右移的origanalEvent.location2,看起来像任何其他密钥都有origanalEvent.location 0