I made a shortcut in my website
alt + 1 = something()
alt + 2 = something()
shift + 1 = something()
...
window.addEventListner("keydown",(e) => {
if(e.key === 1 && e.alt) doSomething();
})
But the problem is when i press shift+1 it become shift + ! the key become special char! And alt(option) + 1 is ¡
Should i make a Key Map for every special chars? Or is there good other way ?
You can use the code property and look for the string "Digit1". That's the purpose of code, to give you a key name unaffected by modifier keys.
Here's a demo of the difference:
window.addEventListener("keydown",(e) => {
console.log(`key = ${e.key}, code = ${e.code}`);
});
Click here, then press 1, and then press !.
So for instance:
if (e.altKey && e.code === "Digit1") {
// ...
}
(Note that it's altKey, not alt, to see if the Alt key is down.)