2015-11-30 51 views
0

我有件事一段代碼:KeyboardEvent.location沒有返回

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

它看起來像e.location沒有返回,「不確定」出現在控制檯。 基本任務是檢測何時按左移和何時右移。 我去了谷歌,發現this,但它不適合我。 有什麼建議嗎?

回答

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   // <--------------------- 

左Shift

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   // <--------------------- 

使用這個,你可以告訴wh按Shift鍵,因爲左移的origanalEvent.location1,右移的origanalEvent.location的值爲2,並且好像其他任何密鑰的origanalEvent.location0

+0

謝謝,它爲我工作。 –