execute JS code after pressing space bar

JavaScript
document.body.onkeyup = function(e){
    if(e.keyCode == 32){
        //your code
    }
}
Source

Also in JavaScript: