2

I'ld like to listen the following keyboardEvent : alt + p

if (event.altKey && event.key === "p") {

  doThis(stuff)

}

Problem is that in OSX (alt+p) === "π"

so I wrote this ugly condition for Mac OSX users

 if (
      (event.altKey && event.key === "π") ||
      (event.altKey && event.key === "p")
    ) {

       doThis(stuff)

}

It works AND it's ugly =D

So if anyone has a better way to handle this please give me a hint !

USEFULL REMINDER: keyCode and charCode are DEPRECATED

Arielo
  • 31
  • 5
  • Note that this issue exists for many special characters on MacOSX** – Arielo Jun 23 '21 at 15:17
  • What about [`event.code`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code)? MDN says that ignores keyboard layout. – zero298 Jun 23 '21 at 15:24
  • @zero298 not necessarily a good thing - it means if people have a different layout (e.g. azerty), you will be receiving "Q" when they type "A" – somebody Apr 26 '23 at 10:15

1 Answers1

1

I figured out that 'keyCode' and 'charCode' are deprecated & have been replaced by '.code' & '.key'. so event.key is just fine =D

Arielo
  • 31
  • 5