i am trying to catch the keypress event on the window (html page opened with an app which uses gecko engine)
function onkeypress(){
alert("key pressed !")
}
i expect this function to be called whenever any button is clicked, when the focus is on window. But the function is not been called. Any idea what is going wrong here? Thanks ...
i am trying to catch the keypress event on the window (html page opened with an app which uses gecko engine)
function onkeypress(){
alert("key pressed !")
}
i expect this function to be called whenever any button is clicked, when the focus is on window. But the function is not been called. Any idea what is going wrong here? Thanks ...
Share Improve this question asked Jun 30, 2010 at 14:58 ganapatiganapati 6252 gold badges12 silver badges24 bronze badges 1- You should accept one of the following answers. They are both correct. – aarona Commented Oct 12, 2010 at 3:51
2 Answers
Reset to default 4You should assign that function to an element:
var elem = document.getElementById('id-here');
elem.onkeypress = function(){
alert("key pressed !");
};
You need to set it as the handler on the window
object if that's what you're after, like this:
window.onkeypress = function() {
alert("key pressed !")
};
This will capture all keypress
events that bubble up (the default behavior, from wherever in the page it happened, with the exception of <iframe>
, videos, flash, etc). You can read more about event bubbling here.