Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added unbind method #31

Merged
merged 7 commits into from

3 participants

David Ben Oakes Craig Campbell
David

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

Ben Oakes benjaminoakes referenced this pull request
Closed

Add unbind method #15

Ben Oakes

This originated with #15

mousetrap.js
@@ -584,6 +584,33 @@ window.Mousetrap = (function() {
},
/**
+ * unbinds an event to mousetrap
+ *
+ * can be a single key, a combination of keys separated with +,
Craig Campbell 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
mousetrap.js
((6 lines not shown))
+ * 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';
Craig Campbell 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
mousetrap.js
((7 lines not shown))
+ * 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'){
Craig Campbell 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
mousetrap.js
((12 lines not shown))
+ *
+ * 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
Craig Campbell Owner

Also remember to add semicolons

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

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

Craig Campbell ccampbell merged commit 049774d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 0 deletions.
  1. +20 −0 mousetrap.js
20 mousetrap.js
View
@@ -696,6 +696,26 @@ window.Mousetrap = (function() {
},
/**
+ * 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.
+ * the keycombo+action has to be exactly the same as it was defined in the bind method
+ *
+ * @param {string|Array} keys
+ * @param {string} action - 'up' for keyup anything else assumes keydown
+ * @returns boolean
+ */
+ unbind: function(keys, action) {
+ if(_direct_map[keys + ':' + action]){
+ delete _direct_map[keys + ':' + action];
+ this.bind(keys, function(){}, action);
+ return true;
+ }
+ return false;
+ },
+
+ /**
* triggers an event that has already been bound
*
* @param {string} keys
Something went wrong with that request. Please try again.