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