keithamus / jwerty

⌨ Awesome handling of keyboard events
http://keithamus.github.io/jwerty
Other
1.21k stars 107 forks source link

Unbind key will not work #47

Open carstenschaefer opened 11 years ago

carstenschaefer commented 11 years ago

Hi there!

I have a modal popup which shows a error message. At the bottom of this popup there is a button to close the popup and do some other things. I'd like to close the popup while pressing enter and setting the focus to the button will not work.

So I have decided to use jwerty for this and I have the following binding:

jwerty.key('enter', function () { $("#btnClose2").click(); });

It works fine. But after the popup is closed the primary behaviour regarding pressing event should be restored so I'd like to unbind this immediately after the click on btnClose2 has occured.

I tried the two following possible solutions: 1)

jwerty.key('enter', function () {
    $("#btnClose2").click();
    $(window).off('keydown.jwerty');
});

2) Alternatively, jwerty.event will return a function which, when run, unbinds the event, so:

var unbind;
$(document).on('keydown', (unbind = jwery.event('enter', function () {
     $("#btnClose2").click();
     unbind();
}))); 

In both cases the unbinding will not work.

Please give me a hint how to solve this.

Many thanks for your help Carsten