Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor keydown doesn't work

I am using meteor to update a document in the database. However keydown does not seem to be working. here is the code.

Template.leaderboard.events({
    'keydown button.inc': function(evt) {
        if (evt.which === 39) {
            Players.update(Session.get("character"), {$inc: {score: 20}});
        }

        Players.update(Session.get("character"), {$inc: {score: 20}});
        //Players.update(Session.get("character"), {$set: {name: 'images/characters/ninja.png'}});
    },
    'keydown': function(evt) {
        if (evt.which === 37) {
            Players.update(Session.get("character"), {$inc: {score: -20}});
            //Players.update(Session.get("character"), {$set: {name: 'images/characters/ninjaleft.png'}});
        }
        Players.update(Session.get("character"), {$inc: {score: -20}});
    }
});

Why isn't my keydown working?

like image 809
Kenneth Ashley Avatar asked Jan 14 '23 22:01

Kenneth Ashley


1 Answers

I've had success attaching keyboard events to textareas and inputs, like the following:

Template.inputArea.events({
  'keydown textarea' : function(e){
     console.log(e.which);
  }
});

I'm not quite as familiar with attaching keyboard events to other DOM elements. I know for window events, I've had to use jQuery to attach keydown inputs.

Template.foo.rendered = function(){
  $(window).on('keydown', function(e){
    console.log(e.which);
  });
};

// and on newer versions 

Template.foo.onRendered( function() {
    $(window).on('keydown', function(e){
        console.log(e.which);
    });
});

Perhaps you could try something like that...

like image 110
bento Avatar answered Jan 19 '23 10:01

bento