4

I'm writing a program with two keyboard modes using jquery.hotkeys.js, I'm seeing some slowdown and flashing when unbinding all keydown events. Is there a better way to do this?

function bindAll() {
    //bind all keystroke events
    $(document).bind('keydown','a', function(evt) {
    //...
    }
    $(document).bind('keydown','b', function(evt) {
    //...
    }
    //etc...
}

function unbindAll() {
     $(document).unbind('keydown');
     return true;
} //end unbinding all keystrokes
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jjclarkson
  • 5,890
  • 6
  • 40
  • 62

1 Answers1

6

jQuery offers you the possibility to create namespaces for events. That looks like:

$(document).bind('keydown.mySpace', function(evt) {
});
$(document).bind('keyup.mySpace', function(evt) {
});

// etc.

You can .unbind() all the events which were added to a namespace then like so:

$(document).unbind('.mySpace');

I'm not aware of the plugin you mentioned, but you should have a look into the docs or even better the source. If it's well designed you should be able to use the jQuery event namespaces for that purpose.

jAndy
  • 231,737
  • 57
  • 305
  • 359