Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added unbind method #31

Merged
merged 7 commits into from

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
Closed

Add unbind method #15

@benjaminoakes

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 +,
@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
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';
@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
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'){
@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
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
@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
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
View
20 mousetrap.js
@@ -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.