ccampbell / mousetrap

Simple library for handling keyboard shortcuts in Javascript
https://craig.is/killing/mice
Apache License 2.0
11.68k stars 969 forks source link

Fail to unbind global shortcut #306

Open lidortal opened 8 years ago

lidortal commented 8 years ago

Hi, how do I unbind global shortcut? I did the following: Mousetrap.bindGlobal('ctrl+s', function() { alert("Hi!"); }); .... Mousetrap.unbind('ctrl+s');

However when pressing ctrl+s I still get the Hi! message. Thanks!

kafeltz commented 8 years ago

Indeed, I tried it too in jsfiddle and I wasn't able to unbind it.

What I could do to temporary solve is to bind it again with empty function callback.

Mousetrap.bindGlobal('ctrl+s', function() { alert("Hi!"); }); Mousetrap.bindGlobal('ctrl+s', function() { /* do nothing */ });

desertdev commented 8 years ago

I have run into the same issue when using bindGlobal and then later needing to clear everything with Mousetrap.reset().

My keyboard shortcuts are built dynamically based on server response, is there a way to get all the current Mousetrap bindings programmatically in order to assign them empty functions?

timmyomahony commented 8 years ago

+1 for this. I want esc to close the sidebar when it's open but I also need esc to do other things throughout the application. I'm using Ember so the keypress bind/unbinds are dynamic as components are rendered. unbinding one component that listens to esc unbinds them all.

davidbarral commented 7 years ago

+1. A use case for this is to have global shortcuts for different pages in an app. It must be possible to unbind them. Also I think It is programmer friendlier to have the methods in pairs bind/unbind bindGlobal/unbindGlobal (I expected this behaviour and it didn't work 😅). We are currently using #353 in some apps. Seems to be working as expected.