简体   繁体   English

有没有办法检测按下Alt键的哪一侧(右侧或左侧)?

[英]Is there a way to detect which side the Alt key was pressed on (right or left)?

Is there a way that we can detect which side the Alt key was pressed on, ie distinguish between left or right Alt ? 有没有办法我们可以检测Alt键被按下哪一侧,即区分左或右Alt I saw somewhere that it's possible with IE with the altLeft and altRight properties of the Event object. 我在某个地方看到IE可以使用Event对象的altLeftaltRight属性。 If that is correct, how can we detect it in Firefox with JavaScript? 如果这是正确的,我们如何在Firefox中使用JavaScript检测它?

This is how it works in IE for altLeft : 这是它在IE中为altLeft

window.onload = function(){
  document.getElementById('id').onkeydown = function(){
    alert(window.event.altLeft);
  }
}

2015 answer 2015年回答

DOM3 added a location property of keyboard events (see also MDN ) (earlier versions had a keyLocation property instead) which does what you want and is implemented in recent versions of all major browsers. DOM3添加了键盘事件location属性 (另请参见MDN )(早期版本有一个keyLocation属性),它keyLocation您的需要,并在所有主流浏览器的最新版本中实现。

Demo: 演示:

 document.getElementById("ta").addEventListener("keydown", function(e) { var keyLocation = ["Standard", "Left", "Right", "Numpad", "Mobile", "Joystick"][e.location]; var message = "Key '" + (e.key || e.keyIdentifier || e.keyCode) + "' is down. Location: " + keyLocation; this.value += "\\n" + message; e.preventDefault(); }, false); 
 <textarea id="ta" rows="10" cols="50">Click on here and press some modifier keys such as Shift</textarea> 

2011 answer 2011年回答

No. In general, it is impossible to distinguish between left and right modifier keys in a cross-browser way. 不可以。通常,不可能以跨浏览器的方式区分左和右修改键。 The shiftLeft , shiftRight , ctrlLeft , ctrlRight , altLeft , altRight properties of window.event are all IE only and no equivalent exists in other browsers. shiftLeftshiftRightctrlLeftctrlRightaltLeftaltRight的属性window.event只是所有IE和没有等效在其他浏览器中存在。

DOM3 added a location property of keyboard events (earlier versions had a keyLocation property instead) but Firefox does not implement this. DOM3添加了键盘事件location属性 (早期版本有一个keyLocation属性),但Firefox没有实现这一点。

Good question. 好问题。 The event object does not contain anything about which alt is pressed but you can try something like this: 事件对象不包含关于按下哪个alt的任何内容,但您可以尝试这样的事情:

var altLeft = false,
    altRight = false,
    time = null;

document.onkeydown = function (e) {
    e = e || window.event;
    //The time check is because firefox triggers alt Right + alt Left when you press alt Right so you must skip alt Left if it comes immediatelly after alt Right
    if (e.keyCode === 18 && (time === null || ((+new Date) - time) > 50)) {
        altLeft = true;
        time = null;
    } else if (e.keyCode === 17) {
        altRight = true;
        time = + new Date;
    }
}

document.onkeyup = function (e) {
    e = e || window.event;
    if (e.keyCode === 18) altLeft = false;
    else if (e.keyCode === 17) altRight = false;
}

document.onclick = function () {
    console.log("left", altLeft, "right", altRight);
}

It works for me in Firefox, anyway the 17 and 18 (that are key codes for alt Right and Left) can change on different browsers or operating systems so you must check them. 它在Firefox中适用于我,无论如何17和18(这是alt Right和Left的关键代码)可以在不同的浏览器或操作系统上进行更改,因此您必须检查它们。

<html>
    <head>
        <title></title>
        <script type="text/javascript">
        flag = 0;
        text = ['else', 'Ctrl', 'AltGr', 'Alt'];

        function key_down(e) {
            var aa = e.keyCode;
            if (aa == 18) {
                event.keyCode = 0;
                event.returnValue = false;
            }
            this_key(aa);
        }

        function this_key(fn1) {
            if (fn1 == 17) {
                flag = 1;
                timer = setTimeout('this_key(1)', 10);
                return;
            }
            if (fn1 == 18 && flag == 1) {
                clearTimeout(timer);
            }

            var aa = 0;

            if (fn1 == 1) {
                aa = 1;
            }
            if (fn1 == 18) {
                if (flag == 1) {
                    aa = 2;
                } else {
                    aa = 3;
                }
            }

            document.getElementById('result').innerHTML = text[aa];
            flag = 0;
        }
        </script>
    </head>

    <body onkeydown="key_down(event);">
        <div id="result" style="font-size: 5em;"></div>
    </body>

</html>

Here is my solution 这是我的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM