0

When I hit shift + enter I want to run a method. I cannot get this to work.

events: {
    'keypress': 'keyPressed'
},

keyPressed: function(event) {
    if (event.keyCode === 13 && event.shiftKey) {
            console.log('TEST');
            this.createGraphicButtonClicked();
        }
    },
}

I have tried keyup, keydown, keypressed body and others with no luck. I have also tried to do it through jQuery in the render method:

this.$(document).keypress(this.keyPressed);

this.$('body').on('keypress', this.keyPressed);

this.$(document).on('keypress', this.keyPressed);

None of these have worked. I have no clue what the deal is. What am I doing wrong?

Soatl
  • 10,224
  • 28
  • 95
  • 153
  • Are you trying to capture the keypress on the document or a specific element? Take a look at this post: http://stackoverflow.com/questions/20716189/keypress-in-backbone-js It says "you are only going to be able to listen to the keypress in whichever element that you have the listener set on (or its children)" so if you're trying to capture the keypress for the whole document you may want to set up the event listener without using the backbone structure. – David Crozier Dec 01 '15 at 22:37
  • Also, if you want to add a listener to the render method, try it without specifying "this": $(document).on("keypress", this.keyPressed); – David Crozier Dec 01 '15 at 22:52
  • I had it on the child. Adding it to the parent resolved the issue. Thanks! – Soatl Dec 01 '15 at 23:27

0 Answers0