0

How to overwrite or remove key events, that is on a website? I'm writing a script for GreaseMonkey and I want to make event on Enter button, but when I press the ENTER button, it triggers function on website.

EDIT 1: Here is the website, that I need to do this http://lockerz.com/auth/express_signup

ncla
  • 803
  • 9
  • 22

3 Answers3

2

One of these two should do it for you. I used the first one, although someone on SO told me the second one will work also. I went for the hammer.

Sorry, first one wasn't a cut and paste answer. I use using it to return up/down arrow control on a website. I changed it so that it identifies keycode 13 instead.

(function() {
    function keykiller(event) {
        if (event.keyCode == 13 )       
        {
            event.cancelBubble = true;
            event.stopPropagation();            
            return false;
        }
    }

    window.addEventListener('keypress', keykiller, true);
    window.addEventListener('keydown', keykiller, true);
})();
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • Second one works, but still triggers the function on website. – ncla Jul 15 '11 at 14:08
  • Not sure if you directly cut and pasted first one. Did you switch it to use keycode 13 for enter key? I think that will work for you. – mrtsherman Jul 15 '11 at 14:14
0

Searching quickly on SO:

Code from there:

 var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
 //Do something
 }
Community
  • 1
  • 1
sdolgy
  • 6,963
  • 3
  • 41
  • 61
0

Without a library, use: http://jsfiddle.net/4FBJV/1/.

document.addEventListener('keypress', function(e) {
    if(e.keyCode === 13) {
        alert('Enter pressed');
        return false;
    }
});
pimvdb
  • 151,816
  • 78
  • 307
  • 352