Search code examples
javascriptdom-eventsgreasemonkeykeypress

Greasemonkey Javascript Key Press


I'm currently trying to make a GreaseMonkey script that will allow a user to press left or right on their keyboard and have that go to a previous comic strip or go to the next strip. I currently have some code up but it's not giving me any results.

function KeyCheck()
{
var KeyID = event.keyCode;
alert(KeyID);
}

document.onKeyDown = KeyCheck();

The code is just for debugging to see if it's actually executing but when I press a key nothing will happen on the page. I'm testing in Firefox also.


Solution

  • So after Googling for a good 30 minutes, I found out GreaseMonkey doesn't support onkeydown out of the box. I had to use a function called "addEventListener". My final code returns the keyCode of a key pressed on the site properly:

    function KeyCheck(e)
    {
    alert(e.keyCode);
    }
    
    window.addEventListener('keydown', KeyCheck, true);