6

How can I clarify ALT+CTRL and ALTGR key press?

I found this code here as possible solution, but it's doesn't work:

if (event.ctrlKey && event.altKey) {

}

This code is true for alt+ctr and for altGr as well.

I have situation like this: for alt+ctrl+e (for example e, it's no matter) I want one thing and for altGr+e another, how can I do this?

If anyone have some idea, please tell me.

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
nix
  • 61
  • 4
  • what do you mean by altgr key, it has [several meaning](http://superuser.com/questions/220071/whats-the-function-of-the-alt-gr-key) :) – Bhojendra Rauniyar Jan 26 '15 at 20:38
  • @BhojendraNepal, he means the right alt. – Mouser Jan 26 '15 at 20:40
  • @Mouser How can you be sure? – Bhojendra Rauniyar Jan 26 '15 at 20:41
  • This is language dependent. When I set my Windows 8.1 to `Nl`. it shows keycodes: `17/18` (meaning `ctrl-alt`), when I set it to `Us` it shows only `18`, meaning `alt`. So `altgr` is just the same thing as `ctrl alt`. – Mouser Jan 26 '15 at 20:43
  • It's simmilar to ctrl+alt in case of windows (ctrl keycode is 17, alt keycode is 18. altgr keycode is 17+18 :) – nix Jan 26 '15 at 20:43
  • @BhojendraNepal, because I'm Dutch and we have a alt gr key on our keyboards :-). – Mouser Jan 26 '15 at 20:43
  • Yes, it's right alt key. – nix Jan 26 '15 at 20:44
  • @nix. It's the same as `ctrl-alt`. Do you want to discriminate between `ctrl-alt` and `altgr`? – Mouser Jan 26 '15 at 20:46
  • @Mouser. yes, exactly – nix Jan 26 '15 at 20:48
  • @nix, according to our constitution article one, discrimination on which ground so ever is deemed illegal. I don't think that's possible. Consider all the variables. It only works on certain languages and keyboard lay-outs. – Mouser Jan 26 '15 at 20:51
  • Adding a generous bounty. That's one of the issues which needs a good solution. – romaninsh Aug 30 '16 at 15:09
  • 2
    Does [Is there is a way to detect which side the Alt Key is Pressed](http://stackoverflow.com/questions/8562528/is-there-is-a-way-to-detect-which-side-the-alt-key-is-pressed) answer this? – Frank Tan Aug 30 '16 at 20:06

3 Answers3

2

You can detect which key is pressed (from right key or left key) by value of location property in event object. If value of location property is 1 (e.location=1) then left key is pressed. if value is 2 then right key is pressed.

Here I have providing my code for RightAlter+RightCtrl+<any_valid_key>

Check this Example

var isRightAltKey=false;
var isRightCtrlKey=false;
var validKeys=['a','s','d','f','g']; //keep empty array if no need to check key


document.addEventListener("keydown", function(e) {
  if(e.key=="Alt"){
       // when right Alter pressed make isRightAltKey true 
       isRightAltKey= (e.location==2); 
       
  }
  else if(e.key=="Control"){
      // when right Control pressed make isRightCtrlKey true, 
      //if you need any ctrl key pressed then simply set isRightCtrlKey= true;
       isRightCtrlKey= (e.location==2); 
  }

  // checking both right key is pressed already or not?
  var isRightKeys= isRightAltKey && isRightCtrlKey;
  
  // validate other keys [optional]
  var isValidKey=((typeof validKeys === "undefined") || validKeys.length==0 || validKeys.indexOf(e.key.toLowerCase())>=0);
  
  if (isRightKeys && isValidKey){
    document.getElementById("detect_key").innerHTML = "RightAlt + RightCtrl + "+e.key; 
  }
  else
  {
    document.getElementById("detect_key").innerHTML="";
  }
}, false);

document.addEventListener("keyup", function(e) {
  if(e.key=="Alt"){
       // when right Alter released make isRightAltKey false
       isRightAltKey= false; 
       
  }
  else if(e.key=="Control"){
       // when right Control released make isRightCtrlKey false
       isRightCtrlKey= false; 
  }
}, false);
<div id="detect_key"></div>

Why attached keyup event listner?

Here we have to detect key location when Ctrl and Alt key is pressed (on keydown event). and we have to store it in flag variable and make it true. when key is released (on keyup event) have to mark as false. Otherwise those flags always remain true. on Next key press it will always true

Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • Please check my answer, let me know it is fulfill your requirement of not? – Haresh Vidja Aug 31 '16 at 12:52
  • @ romaninsh, before give bounty have you checked my solution? and bouty answer handle all cases for key press, It seems he has only detected right side key and left side key. After Right Alt key it will work first time then press with Left Alt key in this case it is also work but as your requirement it should not. – Haresh Vidja Sep 01 '16 at 14:19
1

You can use the location to determined which alt is being pressed. In order to support Alt+Ctrl we'll save the last location of the pressed Alt.

Location = 1 // Left
Location = 2 // Right 

Then, once both Alt and Ctrl are pressed, do your thing. In this example, we'll just write the Alt side in the result div. You can add the "e" pressed condition as well:

if (e.ctrlKey && e.altKey && e.key == "e"){

Example

HTML

<div class="cont">
    Click Alt + Ctrl<br /><br />
    <div id="res"></div>
</div>

Javascript

var lastAltLocation;

document.addEventListener("keydown", function(e) {
   if (e.key == "Alt"){
     lastAltLocation = e.location;
   }
   if (e.ctrlKey && e.altKey){
       if (lastAltLocation == 1){
           document.getElementById("res").innerHTML = "Left";
       }
       if (lastAltLocation == 2){
           document.getElementById("res").innerHTML = "Right";
       }
   } 
}, false);
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
0

Sticking strictly to your question here are the codes for both the required cases:

document.addEventListener ("keydown", function (zEvent) {
    if (zEvent.altKey  &&  zEvent.code === "KeyE") {
        if(zEvent.ctrlKey) {
            //Do Ctrl+Alt+E Stuff Here.
        } else {
            //Do Alt+E Stuff Here.
    }
});

Now breaking down the things going on here. keydown allows you to detect multiple keypresses.

First we check if the Alt and E keys are pressed. If yes, we then go on to check in the Ctrl key is also active and take the appropriate action as needed.

Sunny R Gupta
  • 5,026
  • 1
  • 31
  • 40