harungo
harungo

Reputation: 219

How to catch a user input

I need to catch an user's input, precisely one specific button. I caught this in this way

    Template.main.events({
        'keypress input': function (e) {
            if (e.charCode === 32) {
                console.log("Hit");
            };
        }
    });

and in the template it's something like this

    <template name="main">
      {{test_var}}
      <input type="text">
    </template>

It's works, but i need it without an input box on a page.

Upvotes: 0

Views: 48

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

Template events are restricted to the piece of DOM contained within the template, and within that piece only form elements respond to keyboard events. To capture global keyboard events, you should use jQuery.

Template.main.rendered = function() {
  $(document).on('keypress.mainTemplate', function() {
    ...
  });
};

Template.main.destroyed = function() {
  $(document).off('keypress.mainTemplate');
});

Upvotes: 1

Related Questions