最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onkeypress() not working - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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.

发布评论

评论列表(0)

  1. 暂无评论