2

I have tried following code,

window.onkeypress =  function(event){

   if(event.altKey && (event.keyCode == 99 || event.keyCode == 67)){

      alert("alt + C pressed")
   }
}

How ever It doesn't work!

I tried alert(event.keyCode) per keypress and it seems when I press opt/alt key on Mac, I get a different keycode other than 99 or 67 with the combination.

Whats the correct way to achieve this ?

Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
  • possible duplicate of [How does one capture a Mac's command key via Javascript?](http://stackoverflow.com/questions/3902635/how-does-one-capture-a-macs-command-key-via-javascript) – Waleed Khan Jan 01 '13 at 10:35
  • I have seen and tried that example. command key is working fine. I have a problem with alt! – Amogh Talpallikar Jan 01 '13 at 10:42
  • 1
    Got it, on Mac option plus other key combination generates different characters that's why! opt + c prints ç on TextEdit, thats why a different keyCode. – Amogh Talpallikar Jan 01 '13 at 10:45

1 Answers1

3

Try using the keydown or keyup handler. In that case 'c' = keyCode 67:

window.onkeydown = function(e) {
    e = e || event;
    if (e.altKey && e.keyCode === 67) {
        console.log("alt+c pressed!");
    }
}​

jsfiddle

KooiInc
  • 119,216
  • 31
  • 141
  • 177