I have an application where I use the spacebar to switch functions anywhere in the window. However, if any other button or flag has focus, then it is also clicked.
I tried to do warnDefault (), but this did not work as expected. How can I guarantee that no other item on the screen will be pressed when I press the spacebar?
HTML
<button class="buttons" id="playBtn">PLAY</button>
JS (updated to use the default prevent command to go to space
$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '32') {
if (event.stopPropagation) {
event.stopPropagation();
event.preventDefault();
}
playBtn_DOM.click();
} else if (keycode == '97') {
event.preventDefault();
prevBtn_DOM.click();
} else if (keycode == '100') {
event.preventDefault();
nextBtn_DOM.click();
}
});
As for the answer. When using the default prevention function to capture a space, this solution did not work. I updated the JS code to show that I was trying to include the solution provided there.