Search code examples
javascriptmacoskeyboard-eventskeycodealt-key

How to handle alt+p keyboardEvent on macOS browsers


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


Solution

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