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

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.

Leave a Comment