Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, i.e. distinguish between left or right Alt? I saw somewhere that it's possible with IE with the altLeft and altRight properties of the Event object. If that is correct, how can we detect it in Firefox with JavaScript?

This is how it works in IE for altLeft:

window.onload = function(){
  document.getElementById('id').onkeydown = function(){
    alert(window.event.altLeft);
  }
}
like image 357
Marwan Avatar asked Dec 19 '11 14:12

Marwan


People also ask

How do I know if my right Alt key is working?

Solution: Please go to Control Panel > Region and Language > Keyboards and Language > change Keyboards. You will have multiple language options available. Delete all other language except English US international and US.

What is the difference between left alt and right alt?

There's no difference between the alt, control, and shift keys between the left and right side of the keyboard. The only reason they are existent on both sides are to make for easier more symmetrical typing.

What is the difference between Ctrl key and Alt key?

Ctrl stands for "Control Key". It was originally used to send control characters to terminals. Alt stands for "Alternate Key". It's named so because it enables alternate uses for other keys.


2 Answers

2015 answer

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.

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

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.

DOM3 added a location property of keyboard events (earlier versions had a keyLocation property instead) but Firefox does not implement this.

like image 178
Tim Down Avatar answered Oct 20 '22 20:10

Tim Down


Good question. The event object does not contain anything about which alt is pressed but you can try something like this:

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.

like image 28
mck89 Avatar answered Oct 20 '22 19:10

mck89