Added unbind method #31

Merged
merged 7 commits into from Jul 10, 2012

3 participants

@davoclavo

Simple yet not so fancy solution, without the need to go through all the bind methods to 'parse' the keys.
instead it replaces the callback with an empty function and removes its entry from _direct_map dict

@benjaminoakes benjaminoakes referenced this pull request Jul 9, 2012
Closed

Add unbind method #15

@benjaminoakes

This originated with #15

@ccampbell ccampbell commented on an outdated diff Jul 10, 2012
@@ -584,6 +584,33 @@ window.Mousetrap = (function() {
},
/**
+ * unbinds an event to mousetrap
+ *
+ * can be a single key, a combination of keys separated with +,
@ccampbell
Owner

Hey, you don't have to copy all the documentation from bind. I think that's self explanatory. I would just say

unbinds an event to mousetrap

the unbinding just sets the callback function of that keycombo as an empty function
and deletes the corresponding key in the _direct_map dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ccampbell ccampbell commented on an outdated diff Jul 10, 2012
+ * can be a single key, a combination of keys separated with +,
+ * a comma separated list of keys, an array of keys, or
+ * a sequence of keys separated by spaces
+ *
+ * be sure to list the modifier keys first to make sure that the
+ * correct key ends up getting unbound (the last key in the pattern)
+ *
+ * the unbinding just sets the callback function of that keycombo as an empty function
+ * and deletes the corresponding key in the _direct_map dict
+ *
+ * @param {string|Array} keys
+ * @param {string} action - 'up' for keyup anything else assumes keydown
+ * @returns void
+ */
+ unbind: function(keys, action) {
+ action = action || 'keydown';
@ccampbell
Owner

Action no longer defaults to keydown in the latest version so you can actually just remove this line completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ccampbell ccampbell commented on an outdated diff Jul 10, 2012
+ * a comma separated list of keys, an array of keys, or
+ * a sequence of keys separated by spaces
+ *
+ * be sure to list the modifier keys first to make sure that the
+ * correct key ends up getting unbound (the last key in the pattern)
+ *
+ * the unbinding just sets the callback function of that keycombo as an empty function
+ * and deletes the corresponding key in the _direct_map dict
+ *
+ * @param {string|Array} keys
+ * @param {string} action - 'up' for keyup anything else assumes keydown
+ * @returns void
+ */
+ unbind: function(keys, action) {
+ action = action || 'keydown';
+ if(typeof _direct_map[keys + ':' + action] === 'function'){
@ccampbell
Owner

I would maybe change this line to just be if (_direct_map[keys + ':' + action]) {.

Yours is more explicit, but unless I'm missing something I think it should be safe to just check for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ccampbell ccampbell commented on an outdated diff Jul 10, 2012
+ *
+ * the unbinding just sets the callback function of that keycombo as an empty function
+ * and deletes the corresponding key in the _direct_map dict
+ *
+ * @param {string|Array} keys
+ * @param {string} action - 'up' for keyup anything else assumes keydown
+ * @returns void
+ */
+ unbind: function(keys, action) {
+ action = action || 'keydown';
+ if(typeof _direct_map[keys + ':' + action] === 'function'){
+ delete _direct_map[keys + ':' + action]
+ this.bind(keys,function(){},action);
+ return true
+ }
+ return false
@ccampbell
Owner

Also remember to add semicolons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@davoclavo

Oops, I think I just duplicated the pull request. Did I?

@ccampbell ccampbell merged commit 049774d into ccampbell:master Jul 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment