These events bubble up, so if you’re trying to trigger the event wherever your focus is (ie. not in an input), just bind a handler on window
:
$(window).keypress(function (e) {
if (e.key === ' ' || e.key === 'Spacebar') {
// ' ' is standard, 'Spacebar' was used by IE9 and Firefox < 37
e.preventDefault()
console.log('Space pressed')
}
})
Also see the list of all .key
values.