Amogh Talpallikar
Amogh Talpallikar

Reputation: 12184

How to get Alt + C(or any other key) to work in JavaScript on Chrome for Mac and windows

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 ?

Upvotes: 2

Views: 3878

Answers (1)

KooiInc
KooiInc

Reputation: 122916

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

Upvotes: 3

Related Questions