1

Basically, the Chromebook I'm using has trouble distinguishing between alt keys. As you can see in the image below, there's one on the bottom left (second key from the left, which I'll refer to as ALT1), and a smaller one on the bottom right (5th key from right, ALT2).


(source: computershopper.com)

My code has this if function, which is triggered by the alt and enter key:

if (e.altKey && e.keyCode == 13) {

When I press ALT1 and the enter key, the function works as intended. However, when I press ALT2 and the enter key, all it does is trigger the 'enter' event (So, if I'm in a form, it would submit the form).

To try to fix this, I tried using

if (e.keyCode == 18 && e.keyCode == 13) {

However, neither ALT1 or ALT2 work with that. Any ideas?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mancestr
  • 969
  • 3
  • 13
  • 34

3 Answers3

2

You can test your two alt key code in this page : http://keycode.info/

Maybe the Chromebook is giving a different code for the two alt key.

  • Hi- ironically, this is the site I use to find keycodes in the first place. It gives me a "18" for both, so I'm not sure why this doesn't work – mancestr Oct 30 '16 at 22:13
  • The problem was using `e.keyCode` instead of `e.key`, but check this other tool out: https://keyjs.dev/. It has more detailed information about the different KeyboardEvents and their properties. – Danziger Sep 27 '20 at 07:54
1

In real life, simultanius events rarely exist. What i'm saying is that whatever two keys you try to hit the same time, you won't succeed in doing this at EXACTLY the same time.

you can now use this.

var key1pressed = false;
var key2pressed = false;

$("#somelement").keydown(function(e) {
   if(e.which == 13) {
      key1pressed = true;
   }
   if(e.which == 18) {
      key2pressed = true;
   }
   keyspressed();
}).keyup(function(e) {
   if(e.which == 13) {
      key1pressed = false;
   }
   if(e.which == 18) {
      key2pressed = false;
   }
});

function keyspressed() {
   if(key1pressed == true && key2pressed == true) {
      alert("you pressed these 2 keys together");
   }
}

I guess this is what you mean?

Mart
  • 475
  • 4
  • 21
  • Hey- it's not that hard to get two keys at once. Think of it like Ctrl + C - you hold the Ctrl, and hit the C. My function works 99% of the time- the 1% is because my finger slips and hits the wrong key. The problem isn't with the function- it's with the keycode. For some reason, my computer doesn't recognize the other Alt key as such – mancestr Oct 30 '16 at 22:16
  • 1
    i doubt that what you say is correct. You are asking whether the variable e.keyCode, what can only have ONE value at the time, has 2 different values. namely 18 and 13. This is impossible. you are asking like it is raining and dry at the same time. – Mart Oct 30 '16 at 22:19
  • You actually have a valid point, and I misunderstood your original answer... See, the function doesn't work with ALT2 when I call e.altKey, but when I call e.keyCode == 18, neither works. However, I just tried your code, and it doesn't help- alt2 still isn't recognized as an ALT key – mancestr Oct 30 '16 at 22:21
  • That's odd. This should work like you saw on keycode.info. For the computer the left and right alt button are regarded as only one alt button and therefore it should not matter which of the two alt buttons you hit. http://stackoverflow.com/questions/8562528/is-there-a-way-to-detect-which-side-the-alt-key-was-pressed-on-right-or-left – Mart Oct 30 '16 at 22:26
  • I did just test my own code at my dell laptop. Left alt or Right alt, didn't matter both they gave me the alert message. – Mart Oct 30 '16 at 22:28
  • That's why I'm confused. Keycode.info regards them as the same- however, nothing else does. Calling altKey doesn't work, and niether does e.keycode == "18" – mancestr Oct 30 '16 at 22:31
  • Well, if you copied and tested My code and it doesnt work... then im out, sorry.. – Mart Oct 30 '16 at 22:37
  • It's probably not your code, especially if it worked for you. I'm currently looking up on a lead I found- the Chromebook regards ALT2 as AltGraph, instead of just Alt – mancestr Oct 31 '16 at 00:46
1

Update 2

I used a slightly modified version of my original code.

<script>
window.onkeydown = function (e) {
var enter1 = false;
if (e.keyCode == 13) {
var enter1 = true;
}
if (e.key === 'AltGraph') {
  if (enter1 = true) {

//Do what Alt + Enter does
    console.log("It worked");
  }
}
if (e.altKey && e.keyCode == 13) {

//Do what Alt + Enter does
    console.log("It worked");


  }
};
</script>

This works like a charm: now both ALT1 and ALT2 work.

Original Answer:
By using this code snippet, I was able to figure out the answer.

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>

ALT1 registers as 'Alt', while ALT2 shows up as 'AltGraph'. According to Wikipedia, an AltGraph key is:

AltGr (also Alt Graph, Alt Graphic, Alt Graphics, Alt Grammar, Alt Car, or Right Alt[1]) is a modifier key found on some computer keyboards and is primarily used to type characters that are unusual for the locale of the keyboard layout

Knowing this, I could easily modify my original "if" statement from

if (e.altKey && e.keyCode == 13) {

to this

if (e.altKey && e.keyCode == 13 || e.key === 'AltGraph' && e.keyCode == 13) {



However, I ran into a problem, and haven't been able to figure it out.

 if (e.key === 'AltGraph') {

works just fine: however, when I try

  if (e.key === 'AltGraph' && e.keyCode == 13) {      

it doesn't work. I don't know how, and if anyone can figure out, I'll accept their answer instead. However, for the time being, my answer contains the most relevant info for anyone else who had a similar issue.

UPDATE:

I've figured out the error- however, I can't fix it. This code works fine

if (e.shiftKey && e.key === 'AltGraph') {

It's because I used e.shiftKey instead of e.keycode == 'shift key code'.

However, enter doesn't have the equivalent of this. Using the 'template' below, it works for Shift, Ctrl, and Alt- however, not for enter. Any ideas?

event.[key]Key

For reference, I used these questions to find the answer

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

Detect Alt Gr (Alt Graph) modifier on key press

Community
  • 1
  • 1
mancestr
  • 969
  • 3
  • 13
  • 34