Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keypress events stopped working outside of "input" elements in Meteor after update to 0.5.2

Tags:

meteor

I just found out that after upgrading to Meteor 0.5.2 (from 0.5) event handling for key events ('keypress', 'keydown', keyup') stopped working for me. Other events like ('click' & 'blur') work just fine.

Even in sample apps the code like this doesn't do anything:

Template.someTemplate.events = {
  'keydown' : function(e) {
      console.log(e);
  }
};

The interesting thing is that this code does work (function fires) for keypresses in I'm typing inside an input type="text" or a textarea. But elsewhere - nothing happens.

I'm testing on the latest Crome in Ubuntu 12.10.

Has anybody else experienced the issue?

Thanks, George

like image 806
George Strakhov Avatar asked Nov 21 '25 13:11

George Strakhov


1 Answers

The keydown event works for me for html that is editable. Input fields or contenteditable tags fire the keydown event.

But if you're asking how to handle keydown events on the body, this thread might help:

You can take a look at this thread: https://groups.google.com/forum/?fromgroups=#!topic/meteor-talk/uHy--xIGH8o.

Basically, for now, you can attach an event handler to the body element directly. In the example in the above link, he waits until the template is rendered, and then used jQuery to attach the handler:

Template.myTemplate.rendered = function() { 
        // Assuming you're using jQuery 
        $('body').on('keydown',function() { 
                console.log('key pressed'); 
        }); 
} 

The Meteor team is apparently going to include better support for attaching body level events soon.

like image 84
cmather Avatar answered Nov 23 '25 06:11

cmather