Catching "keydown" event globally?

1,732 views
Skip to first unread message

J Ho

unread,
Oct 2, 2012, 12:12:20 PM10/2/12
to meteo...@googlegroups.com
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.

Gabriel Pugliese

unread,
Oct 2, 2012, 12:46:52 PM10/2/12
to meteo...@googlegroups.com
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.

Iain Shigeoka

unread,
Oct 2, 2012, 1:48:23 PM10/2/12
to meteo...@googlegroups.com
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

David Greenspan

unread,
Oct 2, 2012, 1:50:55 PM10/2/12
to meteo...@googlegroups.com
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.

J Ho

unread,
Oct 2, 2012, 8:36:26 PM10/2/12
to meteo...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages