3

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

2 Answers 2

9

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.

2
  • 5
    Is this still the way to do it? I haven't been able to find any info on this, and I'm having trouble finding anything on non-input keystrokes in Meteor in general. I'm surprised this doesn't come up more often.
    – supertrue
    Oct 20, 2013 at 19:28
  • Not sure if it's the best way to do this but I can confirm it stills works in Meteor 1.1.0.2.
    – the
    Jul 26, 2015 at 14:08
0

You could have simply enclosed

$('body').on('keydown',function() { console.log('key pressed'); });

in meteor.startup function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.