Catching "keydown" event globally?

Showing 1-5 of 5 messages
Catching "keydown" event globally? J Ho 10/2/12 9:12 AM
Hi, I'm stumbling over what seems to be a simple thing: need to catch a keydown event regardless of where a key is pressed.
Putting something like this in a template:

"keydown": (evt)->
      console.log("key pressed: " + evt.which)

works only if the key is pressed inside the "input" element, but I need to handle a specific key being pressed while user is in the page and can't seem to find a way to do it. Any ideas are appreciated!

Thanks!

- J.
Re: [meteor-talk] Catching "keydown" event globally? Gabriel Pugliese 10/2/12 9:46 AM
I would try:
'document keydown' : foo
or
'body keydown' : foo
--

Gabriel Pugliese
+55 11 9-9374-2720
http://www.blogcloud.com.br




- J.

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/meteor-talk/-/3068HWilGh4J.
To post to this group, send email to meteo...@googlegroups.com.
To unsubscribe from this group, send email to meteor-talk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/meteor-talk?hl=en.

Re: [meteor-talk] Catching "keydown" event globally? Iain Shigeoka 10/2/12 10:48 AM
If you have a wrapper template named 'myTemplate' that encloses your entire UI use:

Template.myTemplate.rendered = function() {
        // Assuming you're using jQuery
        $('body').on('keydown',function() {
                console.log('key pressed');
        });
}

Rendered is called after the template is rendered and in the DOM. Once you know the DOM is there, you can use your favorite javascript library to hook into the event on any element you want.

-iain
Re: [meteor-talk] Catching "keydown" event globally? David Greenspan 10/2/12 10:50 AM
This is a known weakness of Meteor's event binding API; we don't expose a way to do this at present.  It's waiting on having some sort of attachment point like Template.BODY / PAGE / DOCUMENT.

-- David


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To post to this group, send email to meteo...@googlegroups.com.
To unsubscribe from this group, send email to meteor-talk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/meteor-talk?hl=en.


Re: [meteor-talk] Catching "keydown" event globally? J Ho 10/2/12 5:36 PM
Iain, Dave, thank you for the hint and the explanation! From the update to the logging thread app it's probably pretty easy to guess where I'm coming from - just want to have the Observatory panel toggling handled not just from pressing the button, but from the everyone's favourite "~" key :) Will play around with Iain's ideas and see if it works at this stage.