Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if .keyup() is a character key (jQuery)

How to know if .keyup() is a character key (jQuery)

$("input").keyup(function() {  if (key is a character) { //such as a b A b c 5 3 2 $ # ^ ! ^ * # ...etc not enter key or shift or Esc or space ...etc /* Do stuff */ }  }); 
like image 823
faressoft Avatar asked Oct 20 '10 12:10

faressoft


People also ask

What is Keyup jQuery?

The keyup() is an inbuilt method in jQuery which is used to trigger the keyup event whenever User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Syntax: $(selector).keyup(function) Here selector is the selected element.

What is Keyup and Keydown in jQuery?

jQuery supports 3 types of keyboard events and which we are : keyup(): Event fired when a key is released on the keyboard. keydown(): Event fired when a key is pressed on the keyboard. keypress:() Event fired when a key is pressed on the keyboard.

What is the difference between Keyup and Keydown?

The keydown event occurs when the key is pressed, followed immediately by the keypress event. Then the keyup event is generated when the key is released.

What is Keyup in Addeventlistener?

The keyup event is fired when a key is released. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .


1 Answers

You can't do this reliably with the keyup event. If you want to know something about the character that was typed, you have to use the keypress event instead.

The following example will work all the time in most browsers but there are some edge cases that you should be aware of. For what is in my view the definitive guide on this, see http://unixpapa.com/js/key.html.

$("input").keypress(function(e) {     if (e.which !== 0) {         alert("Charcter was typed. It was: " + String.fromCharCode(e.which));     } }); 

keyup and keydown give you information about the physical key that was pressed. On standard US/UK keyboards in their standard layouts, it looks like there is a correlation between the keyCode property of these events and the character they represent. However, this is not reliable: different keyboard layouts will have different mappings.

like image 77
Tim Down Avatar answered Oct 23 '22 10:10

Tim Down