PDA

View Full Version : Detect ALT key?


Dylan Leblanc
02-12-2003, 11:41 PM
Can Javascript detect if the ALT key is being pressed?

I don't mind if it would only work using Windows and Internet Explorer.

Quiet Storm
02-12-2003, 11:49 PM
Try This:

Put This In The Body:

<BODY onload="document.body.focus();" onkeydown="AltDown();">

This Is The Function To Detect The ALT:

function AltDown() {
if (window.event.altLeft) {
alert("altLeft Pressed");
}
else {
if (window.event.altKey) {
alert("altRight Pressed");
}
}

//To Cancel The Event
window.event.returnValue = false;

document.body.focus();
}

whammy
02-13-2003, 02:53 AM
Is there an NS solution for this (just out of curiosity, I know how to make the event cross-browser for most keys)?

jkd
02-13-2003, 02:56 AM
The boolean altKey property on a MouseEvent instance.

glenngv
02-13-2003, 07:26 AM
cross-browser solution for detecting modifier keys:
http://www.webreference.com/js/column11/modifierkeys.html

whammy
02-14-2003, 12:03 AM
Cool... I'm sure I will make use of that reference if I ever need to see if someone pressed an ALT key, etc. :)

I generally try to avoid weird stuff like that though...

joeframbach
03-30-2003, 06:51 AM
i'm trying to make a dropdown menu that would be accessible just like in the menu up there *points*, with alt+f to bring down the file menu, etc.

but....
when aalt is pressed, focus goes to the menu and the 'm', for example, when i press alt+m, isnt noticed.
solutions anybody?

liorean
03-30-2003, 01:03 PM
The Mozilla DOM Samples (http://cgi.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/mds.cgi) page has a good interface you can use for determining what KeyEvent interfaces exists in the browser of your choice: KeyEvent:Properties (http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/KeyEvent.htm).

The KeyEvent is one of those that aren't too great when it comes to browser interoperability.

joeframbach
03-30-2003, 06:40 PM
that works great! thanks!