mancestr
mancestr

Reputation: 990

Chromebook Alt Key

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?

Upvotes: 1

Views: 628

Answers (3)

mancestr
mancestr

Reputation: 990

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

Upvotes: 1

Mart
Mart

Reputation: 501

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?

Upvotes: 1

user6599592
user6599592

Reputation:

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.

Upvotes: 2

Related Questions