user3264316
user3264316

Reputation: 332

Meteor - collection find after global keydown?

I have searched all over and Meteor does not seem to be able to do these two things at the same time.

What I want is simple: when any button is pressed (globally), a collection.find(...) is called. It is not doable in Template.events since Meteor does not support global keydown's, and I can't do it in Template.rendered because, for some reason, the collection.find always return nothing.

Any ideas?

Upvotes: 0

Views: 67

Answers (1)

Peppe L-G
Peppe L-G

Reputation: 8345

The following works, but requires an element in the page to have focus, which I guess is the problem in your case.

UI.body.events({
    'keydown': function(event, template){
        console.log('A key is down!')
    } 
})

I guess you have to do it without Meteor. The following should work in modern browsers:

function keydownListener(event){
    console.log('A key is down!')
}

Template.templateName.created = function(){
    document.body.addEventListener('keydown', keydownListener)
}

Template.templateName.destroyed = function(){
    document.body.removeEventListener('keydown', keydownListener)
}

Upvotes: 1

Related Questions