問題描述
在 javascript Event
對象中,有一些布爾值來檢查是否按下了修飾鍵:
In the javascript Event
object, there are some boolean values to check if modifier keys are pressed:
ctrlKey
:CTRL鍵.altKey
:ALT鍵.altLeft
:ALT 左鍵.僅適用于 IE.altGraphKey
:ALTGR 鍵.僅適用于 Chrome/Safari.
ctrlKey
: CTRL key.altKey
: ALT key.altLeft
: ALT left key. Only for IE.altGraphKey
: ALTGR key. Only for Chrome/Safari.
但是,有一些問題:
- 當您按下 ALTGR 修飾符時,IE 和 Chrome 將
ctrlKey
設置為true
并將altKey
設置為true
. - 當您按下 ALTGR 修飾符時,Firefox 將
ctrlKey
設置為false
并將altKey
設置為true
,因為只有 ALT已按下. - Chrome 有
altGraphKey
屬性,但它始終是undefined
.
- 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
.
問題:如何區分 ALT+CTRL 或 ALTGR 按鍵?特別是在 Chrome 中.
Question: how can I difference between an ALT+CTRL or an ALTGR key press? Specially in Chrome.
推薦答案
webkit 瀏覽器中的 altGraphKey 似乎不再存在(截至 2013 年 9 月),并且 Firefox 的行為發生了變化.AltGr 鍵的瀏覽器行為當前似乎是:
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
也就是說,它們當前都是一致的(除了 IE8,它仍然始終不一致).
Which is to say, they are all currently consistent (apart from IE8, which remains consistently inconsistent).
以下代碼段應該在現代瀏覽器中捕獲 Alt Gr - 但不是 Alt 或 Ctrl -.但是,您將需要 IE8 的特殊情況:
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)修飾符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!