Search code examples
javascriptfirefox-addondom-events

eventListener not catching keypress event with Javascript


I've written some code to check if a user launches a search on Google (clicking on the Search button or typing Enter).

My code works fine (it's a Firefox extension) except I can't get the code of the key pressed for an unknow reason.

my code :

window.addEventListener("submit", function() { myExtension_with_click.init(); }, false);
window.addEventListener("keypress", function() { myExtension_with_keypress.init(); }, false);

var myExtension_with_click = {
    init: function() {          
         alert("This works");
    }
}

var myExtension_with_keypress = {
    init: function() {
             alert("This works")
             if (window.event.keyCode == 13) {
                 // This doesn't work
                 alert("This doesn't work");
             }
    }
}

Solution

  • A simple solution is to use the event "change" in the event listener !