Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript alt key

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.

like image 822
Riera Avatar asked Jul 10 '12 11:07

Riera


People also ask

How do I find my Alt key?

On all keyboards, the Alt key is located in the bottom row, directly to the left of the space bar.

What is event altKey?

The KeyboardEvent. altKey read-only property is a boolean value that indicates if the alt key ( Option or ⌥ on macOS) was pressed ( true ) or not ( false ) when the event occurred.

Which method used to capture Alt Ctrl Meta or Shift keys?

A KeyEventArgs, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each KeyDown or KeyUp event.

Which of the following keyword properties is used to return whether or not the Alt key was pressed when an event was triggered in HTML?

The altKey property returns a Boolean value that indicates whether or not the "ALT" key was pressed when a key event was triggered.


3 Answers

maybe like this

document.onkeydown = keydown;

function keydown(evt) {
    if (!evt) evt = event;
    if (evt.altKey) {
        console.log('alt');
    }
} // function keydown(evt)​
<input type"text" onkeydown="keydown" />
like image 55
Ricky Avatar answered Oct 31 '22 14:10

Ricky


Working Demo

document.onkeyup = KeyCheck;       

function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 18:
      document.Form1.KeyName.value = "Alt";
      // do your work on alt key press....
      break; 

      case 17:
      document.Form1.KeyName.value = "Ctrl";
      break;
   }
}

And your html may be like that

<form name="Form1">

<input type="text" name="KeyName" value="" />

</form>​

Note: If you want to get the alt event on other control/type than modify it with your requirements.

like image 7
Talha Avatar answered Oct 31 '22 15:10

Talha


I don't know if this is the most elegant solution, but it worked fine.

$(function()
{
    //Flag to check if another key was pressed with alt
    var vAnotherKeyWasPressed = false;
    //ALT keycode const
    var ALT_CODE = 18;

    //When some key is pressed
    $(window).keydown(function(event)
    {
        //Identifies the key
        var vKey = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        //The last key pressed is alt or not? 
        vAnotherKeyWasPressed = vKey != ALT_CODE;
    });

    //When some key is left
    $(window).keyup(function(event)
    {
        //Identifies the key
        var vKey = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

        //If the key left is ALT and no other key is pressed at the same time...
        if (!vAnotherKeyWasPressed && vKey == ALT_CODE)
        {
            //Focus the menu
            $('#myMenu').focus();
            //Stop the events for the key to avoid windows set the focus to the browser toolbar 
            return false;
        }
    });
});
like image 4
Riera Avatar answered Oct 31 '22 14:10

Riera