Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable certain key's default action

function keypressCheck() {
    var keyID = event.keyCode;

    //space pressed
    if (keyID == 32) {
        anotherFunction();
    }
}

I want anotherFunction() to run when the space bar is pressed without the default action of the page scrolling to happen. is there any way to do this?

like image 653
ptigers9 Avatar asked Sep 06 '25 13:09

ptigers9


1 Answers

It should work. Just to make sure, try this:

function keypressCheck(e) { 
    var e = window.event||e; // Handle browser compatibility
    var keyID = e.keyCode;
    //space pressed
    if (keyID == 32) {
        e.preventDefault(); // Prevent the default action
        anotherFunction();
    }
}
like image 143
JCOC611 Avatar answered Sep 09 '25 18:09

JCOC611