I’m writing code in JavaScript, targeting Chrome.
Now, consider what happens when:
- I press Space/Backspace on a webpage vs
- I press Space/Backspace on a textbox
BY using (goog.events.listen js/document.body …) I can listen for Space/Baskspace events and react on them. However, the browse still processes them i.e. when I press Space the browser still scrolls down and when I press Basckspace, the browser goes to the previous webpage.
I would like to prevent this “default behavior” — i.e. I want to process the Space/Backspace events, and I want the browser to ignore them.
Thanks!
Return false from the event handler to cancel the event.
Please don’t kill keyboard shortcuts globally like this. It hurts usability. Only prevent default behaviour when some custom widget on the page has focus, and this element can lose focus using a keyboard shortcut like tab.