Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is keydown triggering but keyup is not?

$(document).on('keydown', function(e) {
     if (e.shiftKey) {
          $('body').append('test1');
     }
});
$(document).on('keyup', function(e) {
     if (e.shiftKey) {
          $('body').append('test');
     }
});

keyup never triggers for me but keydown does, why is that?

like image 420
cantread Avatar asked Nov 12 '13 16:11

cantread


1 Answers

event.shiftKey always return false on keyup.

Check for keyCode === 16 instead (which is the shift key code, on keyup):

$(document).on('keydown', function(e) {
     if (e.shiftKey) {
          $('body').append('test1');
     }
}).on('keyup', function(e) {
     if (e.keyCode === 16) {
          $('body').append('test');
     }
});

Demo: http://jsfiddle.net/maniator/eyX5N/

like image 160
Naftali Avatar answered Sep 21 '22 03:09

Naftali