'Backbone window keypress event?

I know that I can use the window.keypress event

$(window).keypress(function(e) {
    alert('hello world');
});

But I was wondering if there was a way to use the backbone events to catch a keypress anywhere in the window?

I cannot do it on a view, because my page will contain multiple views.



Solution 1:[1]

Backbone's event handlers only works in its own view. Doesnt affect window context. You need to setup inside initialize i assume.

var MyView = Backbone.View.extend({
    initialize : function(){
        $(window).on("keypress", this.windowKeyPress)
    },
    windowKeyPress : function(){
        // handler.
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anakin