keycode - Pressing specific keys to do certain things in JavaScript works in Chrome but not Firefox -


i'm using 2 functions detect whether pressed key matches want, , if so, something. works charm in chrome, neither function works in firefox. there way can make work there too?

document.onkeypress = function() {    if (event.charcode == 49) {      alert("you pressed 1!");    }    if (event.charcode == 50) {      alert("you pressed 2!");    }    if (event.charcode == 51) {      alert("you pressed 3!");    }    if (event.charcode == 52) {      alert("you pressed 4!");    }  }    document.onkeydown = function() {    if (event.keycode == 122) {      alert("you pressed f11!");    }  }
<span>press 1, 2, 3, 4 , f11 launch different events.</span>

you forget declare event argument. perhaprs chrome automatically. works in both browsers-

document.onkeypress = function(event) {   if (event.charcode == 49) {     alert("you pressed 1!");   }   if (event.charcode == 50) {     alert("you pressed 2!");   }   if (event.charcode == 51) {     alert("you pressed 3!");   }   if (event.charcode == 52) {     alert("you pressed 4!");   } }  document.onkeydown = function(event) {   if (event.keycode == 122) {     alert("you pressed f11!");   } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -