2014-02-26 77 views

回答

23

在支持DOM3的较新浏览器中,您可以使用event.location来检查位置。

the DOM3 spec,存在用于位置,DOM_KEY_LOCATION_STANDARDDOM_KEY_LOCATION_LEFTDOM_KEY_LOCATION_RIGHT,和DOM_KEY_LOCATION_NUMPAD定义4个常数。

在这种情况下,你可以这样做:

if (event.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT){ 

} else if (event.location === KeyboardEvent.DOM_KEY_LOCATION_RIGHT){ 

} 
+0

你能告诉我完整的代码吗?我没有得到如何执行它 –

+0

您可以在浏览器中的键盘事件处理程序中进行此操作,其中事件处理函数的第一个参数是'event'。 – loganfsmyth

+0

它确定。我做了另一种方式。 http://stackoverflow.com/questions/40320597/detect-left-or-right-shift-key-in-keypress-jquery/40320730?noredirect=1 –

2

Internet Explorer中能够区分左右Shift与shiftLeft属性:

shiftLeft property (event)

否则,他们是无法区分的。

+0

有一些区别。检查在这个问题上的PLUNK的例子:http://stackoverflow.com/questions/30218752/how-to-detect-shift-right-arrow-some-other-key-in-angular 相同的代码工作正常左移,右移不起作用 – gandra404

0

最简单的方式做到这一点

$(document).ready(function(){ 
 
    $("html").keydown(function(e) { 
 

 
     if (e.shiftKey) { 
 
     if (event.location == 1) console.log('left shift'); 
 
     if (event.location == 2) console.log('right shift'); 
 
     } 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

注意:你有你什么时候点击里面的白色空间运行代码片段来激活键盘按键。这在Chrome和Safari中进行了测试。

相关问题