detecting two keys pressed down
HTML
<h1>hi</h1>
<textarea cols=80 rows=10></textarea>
JavaScript
(function ($) {
$(
function () {
/* somethign odd seems to be happening. if you hit cmd+a and hold it both are registered properly. but if you release a the keyup for a is not registered. if you release cmd first then a then the keyup for both is registered. only tried it on chrome */
var checkCt = function (e, ct,k) {
$('textarea').val('on ' + e + ' the count is: ' + ct);
console.log(e,ct,k,keysDown);
},
keyct = 0,
keysDown = {},
thisKey, lastKey;
$('body').on('keydown', function (e) {
console.log(e.keyCode ,' is going down');
thisKey = e.keyCode;
if (!keysDown[thisKey]) {
keysDown[thisKey] = thisKey;
keyct++;
checkCt('down', keyct, e.keyCode);
}
lastKey = thisKey;
return false;
})
$('body').on('keyup', function (e) {
console.log(e.keyCode ,' is going up');
thisKey = e.keyCode;
if (keysDown[thisKey]) {
keysDown[thisKey] = undefined;
keyct--;
checkCt('up', keyct,e.keyCode);
}
});
}); // on ready
})(jQuery);