views:

29

answers:

1
$("#input").keyup(function(){
 console.log($(this).val());
})

Problem is when one is typing slowly "a" & "b" the above code will result in "a","b" in console log. But when somebody does it quickly the result is "ab","ab". It's easier to do with letters which are near on a keyboard eg. "e"&"r". How to avoid it?

Events keydown and keypress does not suffer with problem of "quicktypers", but they are also to early in this case, because value of an input does not contain last typed letter when they occur. Or maybe there is a way to get this lest letter some how?

+3  A: 

Well, the problem is not really fast typing, but actually when the key up event happens. Consider this:

  • a is pressed
  • b is pressed
  • a is released
  • b is released

No matter how slowly this is done, you will not get the key up event until the first key is released. There is obviously no way around this, you can't cause the key up event before the key is released.

If you just want the key that is pressed in a key press event, you can get it from the event object. Example:

$('#input').keypress(function(e){
  console.log(e.which);
});
Guffa