madrobby/keymaster — pull request #32 by lowe (2011-11-06 04:58:09+00:00)
Title: reset modifiers to false on window focus. fixes alt+tab, tab switching, and related issues

proposed fix for issue #16, including these example problems:

* when I alt+tab away from the browser to another application, the window doesn't get the keyup events, so _mods[alt] stays true after I navigate back. to reproduce:

key('d', function () {
console.log('pressed d!');
});

alt+tab to another program.
navigate back to the browser.
press 'd', nothing happens.

* same issue when I use shift+command+] to switch browser tabs on mac (or any other tab-switching shortcut that involves modifiers).

* here's a sweet one :) same issue when i do:

key('ctrl+c', function () {
alert('pressed ctrl+c!');
}) ;

alert() takes focus away from the window, so after i click 'ok', alert() will keep getting called every time I press 'c' without the modifier.

fix:

reset _mod values to false every time the window is focused. i experimented with document vs window, and focus vs blur, and window focus is the only event that works for every scenario. tested on Chrome/FF/Safari.