2

I don't understand if anything is wrong in this idea but the Backbone Views just don't trigger keypress, keyup events. I have created a simple shopping list app is JsBin for you to inspect. In the chrome inspector the ul of the view shows the keyup event but it does not occur when i hit some keys in the keyboard. I need the idea to navigate Treeview using the keyboard events

Jsbin

http://jsbin.com/arucom/2/edit

Community
  • 1
  • 1
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
  • http://stackoverflow.com/questions/6033010/how-to-capture-the-key-event-from-a-view – rkw Jul 17 '12 at 06:43
  • @rkw Hi thanks for the duplicate. So this isn't possible with what i got now ? If i had links instead will it be possible ? – Deeptechtons Jul 17 '12 at 07:26
  • the focus would need to be on the links. the easiest way is to [hook onto the document](http://jsbin.com/arucom/3/edit) like the suggestion mentioned, but you would have to remember to unbind the event when the view is removed. – rkw Jul 17 '12 at 08:05
  • @rkw I don't get it. How come `document` when it does not have any input elements can get to trigger these events but my `view` does not.There must be some way to do this, let me try it out and post an answer – Deeptechtons Jul 17 '12 at 08:17

1 Answers1

7

In addition to the question @rkw linked You might want to have a look at this SO question Why audio events are not firing with BackboneJS but others are?

Basically backbone.js uses delegation to bind events, which only works with delegate-able events.

You can bind to the keypress manually in the initializer

 initialize: function () {
        _.bindAll(this);
          $(document).bind('keyup', this.navigate);
    },
Community
  • 1
  • 1
Jack
  • 10,943
  • 13
  • 50
  • 65