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

4

2 回答 2

4

您应该将该函数分配给一个元素:

var elem = document.getElementById('id-here');

elem.onkeypress = function(){
  alert("key pressed !");
};
于 2010-06-30T15:00:30.983 回答
4

如果这是您所追求的,您需要将其设置为window对象的处理程序,如下所示:

window.onkeypress = function() {
  alert("key pressed !")
};

这将捕获所有keypress冒泡的事件(默认行为,从页面中发生的任何地方开始,除了<iframe>、视频、flash 等)。您可以在此处阅读有关事件冒泡的更多信息

于 2010-06-30T15:01:19.873 回答