dmauro / Keypress

A keyboard input capturing utility in which any key can be a modifier key.
http://dmauro.github.io/Keypress/
Apache License 2.0
3.18k stars 313 forks source link

Restricting input to specific DOM elements #14

Closed ashwinr closed 10 years ago

ashwinr commented 11 years ago

Is there a way to specify the root DOM element for which all shortcuts apply? There are cases where the same shortcut can trigger different callbacks depending on which DOM element is focused for the keyboard input. Conversely, if there's a way we can also namespace the shortcut definitions, that will also solve this issue.

dmauro commented 11 years ago

Currently there is not, you would have to manually manage that yourself by turning on and off the dictionaries of keyboard shortcuts you want to use for the various DOM elements. I would like to make this much easier though by making keypress instance based instead of the global way it operates now.

ashwinr commented 11 years ago

How would it help if keypress is instance-based? Wouldn't all instances still attach to document.body? It would be nice if we could tell an instance to listen on a specific element, defaulting to document.body.

dmauro commented 11 years ago

It would help so you could create multiple instances and turn them on and off based on any number of things, not just restricted to which DOM elements are active. I probably won't be adding specific DOM binding because it seems a bit out of the scope of the project.

dmauro commented 10 years ago

Actually, as I'm working on this, it's very easy and makes sense to implement. There will be a Keypress Listener class that you instantiate and you can pass in an element to make it something other than document.body.

ashwinr commented 10 years ago

cool, thanks!

dmauro commented 10 years ago

I just pushed v2.0 which has a new API. When you create your keypress.Listener instance, pass the DOM element you'd like to bind to into the constructor. Thanks for the suggestion :)