在按键上检测Alt Gr(Alt Graph)修改器 [英] Detect Alt Gr (Alt Graph) modifier on key press
问题描述
在javascript Event
对象中,有一些布尔值用于检查是否按下了修饰键:
ctrlKey
: CTRL 键
altKey
: ALT 键。
altLeft
:< kbd>左键。仅适用于IE。
altGraphKey
: ALTGR 键。仅限于Chrome / Safari。
然而,有一些问题:
中ctrlKey
来真
和方altKey
到真正
当你按下ALTGR修改。
中ctrlKey
至 false
和 altKey
至 true
当您按下ALTGR修饰符时,因为只有ALT被按下。
altGraphKey
属性,但它总是未定义
问题:如何可以我之间差的 ALT + CTRL 或 ALTGR 键按下?特别是在Chrome中。
webkit浏览器中的altGraphKey不再显示存在(截至2013年9月)已经改变。 AltGr键的浏览器行为目前看起来是:
$ b $ ul
也就是说,它们目前都是一致的始终不一致)。
以下片段应该在现代浏览器中捕捉Alt Gr - 但不是Alt或Ctrl - 。然而,你需要IE8的一个特例:
if(event.ctrlKey&& event.altKey){
//显示为Alt Gr
}
In the javascript Event
object, there are some boolean values to check if modifier keys are pressed:
ctrlKey
: CTRL key.altKey
: ALT key.altLeft
: ALT left key. Only for IE.altGraphKey
: ALTGR key. Only for Chrome/Safari.
However, there are some issues:
- IE and Chrome set
ctrlKey
totrue
andaltKey
totrue
when you press the ALTGR modifier. - Firefox sets
ctrlKey
tofalse
andaltKey
totrue
when you press the ALTGR modifier, as only ALT has been pressed. - Chrome has the
altGraphKey
property, but it is alwaysundefined
.
Question: how can I difference between an ALT+CTRL or an ALTGR key press? Specially in Chrome.
The altGraphKey in webkit browsers no longer appears to exist (as at September 2013) and the behaviour of Firefox has changed. Browser behaviours for the AltGr key currently appear to be:
- Webkit (Chrome) - ctrlKey: true, altKey: true
- IE 8 - ctrlKey: false, altKey: true
- IE 10 - ctrlKey: true, altKey: true
- Mozilla (Firefox) - ctrlKey: true, altKey: true
Which is to say, they are all currently consistent (apart from IE8, which remains consistently inconsistent).
The following snippet should catch Alt Gr - but not Alt or Ctrl - in modern browsers. You will need a special case for IE8 however:
if (event.ctrlKey && event.altKey) {
// Appears to be Alt Gr
}
这篇关于在按键上检测Alt Gr(Alt Graph)修改器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!