AltGr视为Alt + Ctrl

时间:2016-04-26 13:31:12

标签: javascript linux windows input keyboard

在Windows上,我希望 AltGr 键能够成功构成变音字符,但是在幕后看来,windows AltGr 键相当于<从这里可以看出kbd> Alt + Ctrl [1](使用该工具解释此页面上的键盘事件)。这只是在Windows上的行为;在linux上AltGr 分成 Alt + Ctrl 这样。

如何在Windows上区分正常 AltGr 操作与 Alt + Ctrl 操作?

[1] http://javascript.info/tutorial/keyboard-events

1 个答案:

答案 0 :(得分:0)

modern browsers中,您可以使用location property of the keyboard event来确定是否按下左侧或右侧Alt键(右侧是AltGr)。

document.getElementById("test").addEventListener("keydown", function(e) {
    // For pretty output in the demo
    var locations = {"1": "Left", "2": "Right"};
    var keys = {"17": "Ctrl", "18": "Alt"};

    // Checking e.keyCode and e.location
    document.getElementById("test").value = 
        "Key: " + (keys[e.keyCode] || e.keyCode) + 
        ", Location: " + (locations[e.location] || e.location);

    e.preventDefault();
});
document.getElementById("test").focus();
<input id="test" type="text" style="width: 100%;" value="Click here and test alt, ctrl and altgr"/>

相关问题