How can I handle key events in a meteor?

According to the docs, I should be able to handle key events, such as keypress, in the same way that I can handle the event click, but I feel that something is missing.

I have done the following:

> meteor create keypressTest
> cd keypressTest
> sed -e 's/click input/keypress body/' -i .bak keypressTest.js
> meteor

But when I press the keys, nothing is displayed on the console, as when processing the click event.

Are there any examples of working with key manipulators in a meteor? I know that I can make a workaround in jquery, but I prefer to stick with clean template events if I can.

+5
source share
4 answers

I missed these two requirements for using key events in eventmap:

  • The target must be contained in the template (like other events)
  • ,

, click * , , .

+5

, keypress , textinput.

, keydown keyup , , keypress. , :

  • keydown
  • keypress
  • keyup

, keydown; , keyup. , , - , , ...

+10

I can find my keypress events

Template.myTemplate.events({
    'keypress input': function(e) { console.log('key', e); }
});

or in a more convenient example

Template.myTemplate.events({
      'keyup input': function(event) {
      if (event.which === 13) {
         alert('you hit enter');
         event.stopPropagation();
         return false;
      }
   },
    ...

@tom, I received nothing from textinput :(

+8
source

Enter will return event.charCode = 0, use instead event.keyCode, it will return 13.

'keypress input': function(event) {
    if (event.keyCode == 13) {
        alert('you hit enter');
        event.stopPropagation();
        return false;
    }
}
+1
source

All Articles