keithamus / jwerty

⌨ Awesome handling of keyboard events
http://keithamus.github.io/jwerty
Other
1.21k stars 109 forks source link

Better support for mapping multiple keys #11

Open marcbourlon opened 12 years ago

marcbourlon commented 12 years ago

Is there a way to retrieve THE key pressed in case of single key? Because I'm not sure I wouldn't interfere with jwerty events, I'm trying to use jwerty for all my keys management. In which case, it happens I need to get the key pressed. How can I? I can do "if jwerty.is('x')", but it can be tedious.

keithamus commented 12 years ago

At current there is no "easy" way to do mass key bindings, other than using jwerty.is.

I have been considering the issue of mass-keybindings though, before this issue. My proposal is to allow an object mapping of keys to functions, allowing you to do something like this:

jwerty.key('myinput', {

    'ctrl+shift+a': false,
    'ctrl+shift+x': function () { console.log('ctrl shift x'); },
    'ctrl+shift+p': myFunction

});

However for repetitive tasks, that mostly do the same thing with a few minor exceptions, this doesn't really improve anything.

I'd like to see your use code though, how you're using it and what you think would improve the terseness.

marcbourlon commented 12 years ago

HI Keith,

Thanks for the answer. While posting the issue, I thought that maybe I was not doing things optimally. I'm doing a webapp, in which I use key combos (and for this, jwerty just rocks), but also single keypresses. I don't feel comfortable at adding my own keydown events, so I had to do it to detect single dead keys, as posted in another issue (how, with jwerty, would you use a dead key to enable a specific mode of your app? Like: normal mouse action is drawing, but if I press Ctrl, same mouse action will result in moving elements? I need a keydown and a keyup callbacks, and as of now, had to do with my own (crappy) code. Though this is another issue, it also comes to the fact I need to "complete" Jwerty, which is a bit less elegant for me. As for the current issue, here's my need: I use the same callback for two keys (v and h namely), and use the key as an object property, to make it simple. I have something like:

Jwerty.key('v/h', myCallback);

function myCallback(e) { var key=Key.getKey(e);

MyObject[key] = "foobar"; }