javascript alt key

Riera picture Riera · Jul 10, 2012 · Viewed 28k times · Source

We are creating a web user interface that looks like a desktop window. Now we need to handle the Alt key. When Alt key is pressed the focus goes to the upper menu.

In Javascript, how to get the event for Alt key when ONLY Alt key is pressed? I need to ensure that no other key was pressed at the same time.

Thanks in advance.

Answer

Ricky picture Ricky · Jul 10, 2012

maybe like this

document.onkeydown = keydown;

function keydown(evt) {
    if (!evt) evt = event;
    if (evt.altKey) {
        alert('alt');
    }
} // function keydown(evt)​