keymaster
keymaster copied to clipboard
Redefining shortcut should replace behavior
If you define a function for a shortcut that has already been defined results in possibly unwanted behavior. Both the old and the new function are performed when the shortcut is activated.
This could be an intended behavior, but according to me, it doesn't seem logical. If you want to be able to do multiple things with one shortcut, you should define them in one function.
If a shortcut's function doesn't replace the previous one, you should at least be able to remove or reset the shortcut.
I'm guessing your case is probably dissimilar to mine but I've figured out that calling key.deleteScope('all'), or whatever scope you're assigning your events in, will work fine. Granted you have to reassign all other keys in addition to the one you'd like to replace, but that could be accomplished by grouping those calls in methods for each context.
This tripped me up just now, but I doubt that it should be changed. In jQuery, if you do this:
handler = -> console.log 'fired'
$(document).on 'click', handler
$(document).on 'click', handler
$(document).on 'click', handler
Then each click will run handler()
3 times.
$(document).off 'click', handler
…will remove all three listeners.
Perhaps what we need is an unassignKey()
method:
# Assign a key
key 'space', 'myScope', handler
# Later on…
key.unassign 'space', 'myScope' # Removes all handlers associated with the spacebar
# Or…
key.unassign 'space', 'myScope', handler # Removes all instances of the handler function for the spacebar