OscarGodson / jKey

Key shortcuts made simple
http://oscargodson.github.com/jKey/
185 stars 40 forks source link

Key patterns (like the konami code) #3

Open rushfrisby opened 13 years ago

rushfrisby commented 13 years ago

Can you add support for key chaining? For example to fire when user presses up,up,alt+shift+m ...or something more complicated like the konami code.

OscarGodson commented 13 years ago

Right now jKey does support "chains" but what we defined as a chain is:

alt+shift+t

You mean more of supporting "Key patterns" more specifically correct? The above example you gave would tell jKey to attach up and alt+shift+m. This is done like this to be like jQuery and CSS.

Do you have another example? What if it were wrapped in like square brackets like?

[up,up,down,down]

Like that?

kameelyan commented 13 years ago

First off, let me say this is a fantastic little plugin for jquery. Secondly, it's totally possible to do what you're requesting, but just requires some thought. Let's say you want to chain the following combination: J, K, E, Y. For each of those key functions, just populate an array adding the last pressed key, then check the array against your target chain "jkey". As soon as they don't match, clear the array and then add the last letter clicked (representing starting over). As long as it partially matches do nothing (basically maybe they've only gotten as far as "jke", and finally as soon as it fully matches do whatever event you want to fire.

OscarGodson commented 13 years ago

@kameelyan Yeah, i was thinking if I put it in square brackets it'd trigger jKey to look for that pattern. Are you fluent in JS? You wanna do this and send a pull request?