javascript可以告诉左右键之间的区别吗? [英] Can javascript tell the difference between left and right shift key?

查看:175
本文介绍了javascript可以告诉左右键之间的区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数情况下,这是一项健全性检查。两个shift键的关键代码是16.这是否意味着在浏览器中区分左右移位事件实际上是不可能的?

Mostly this is a sanity check. The key code for both shift keys is 16. Does that mean it is actually impossible to distinguish a left and right shift events in a browser?

推荐答案

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

In newer browsers supporting DOM3 you can use event.location to check the location.

DOM3规范,为位置定义了4个常量, DOM_KEY_LOCATION_STANDARD DOM_KEY_LOCATION_LEFT DOM_KEY_LOCATION_RIGHT DOM_KEY_LOCATION_NUMPAD

In the DOM3 spec, there are 4 constants defined for location, DOM_KEY_LOCATION_STANDARD, DOM_KEY_LOCATION_LEFT, DOM_KEY_LOCATION_RIGHT, andDOM_KEY_LOCATION_NUMPAD.

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

In this case, you can do:

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

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

}

这篇关于javascript可以告诉左右键之间的区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆