Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the pressed key will produce a character inside an <input> text-box?

I have a regular text-box:

<input type="text">  

I use jQuery to handle key-related events:

$("input:text").keydown(function() {     // keydown code }).keypress(function() {     // keypress code }).keyup(function() {     // keyup code }); 

The user focuses on a text-box and presses various keys on his keyboard (the usual ones: letters, numbers, SHIFT, BACKSPACE, SPACE, ...). I need to detect when the user presses a key that is going to increase the length of the text-box value. For example, the "A" key will increase it, the "SHIFT" key wont.

I remember watching a lecture by PPK where he mentioned the difference between those two. It has something to do with the event - keydown vs. keypress - and possibly with the event properties - key, char, keyCode.

Update!

I need to know this information within the keydown or keypress handlers. I cannot wait for the keyup event to occur.

Why I need this:

I have a text-box which size dynamically changes based on the user input. You can have a look at this demo: http://vidasp.net/tinydemos/variable-size-text-box.html

In the demo, I have a keydown and keyup handler. The keyup handler adjusts the text-box size based on the input value. However, the keydown handler sets the size to be 1 character larger then the input value. The reason I do this is that if I didn't, then the character would overflow outside the text-box and only when the user would let go of the key, the text-box would expand. This looks weird. That's why I have to anticipate the new character - I enlarge the text-box on each keydown, ergo, before the character appears in the text-box. As you can see in the demo, this method looks great.

However, the problem are the BACKSPACE and ARROW keys - they will also expand the text-box on keydown, and only on keyup the text-box size will be corrected.

A work-around:

A work-around would be to detect the BACKSPACE, SHIFT, and ARROW keys manually and act based on that:

// keydown handler function(e) {     var len = $(this).val().length;     if (e.keyCode === 37 || e.keyCode === 39 ||         e.keyCode === 16) { // ARROW LEFT or ARROW RIGHT or SHIFT key         return;     } else if (e.keyCode === 8) { // BACKSPACE key         $(this).attr("size", len <= 1 ? 1 : len - 1);     } else {         $(this).attr("size", len === 0 ? 1 : len + 1);     } } 

This works (and looks great) for BACKSPACE, SHIFT, ARROW LEFT and ARROW RIGHT. However, I would like to have a more robust solution.

like image 335
Šime Vidas Avatar asked Nov 14 '10 21:11

Šime Vidas


People also ask

How do you check if pressed key is enter?

To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event.

Which event is triggered only once when the keyboards key is pressed?

The keypress event is normally triggered when a key on the keyboard is being pressed down. The method keypress() attaches an event handler which is triggered when the key is being pressed down, or triggers the keypress event. When modifier keys are pressed (shift, ctrl, alt, etc.), the event will not fire.

How do I find the Enter key in HTML?

And the following JavaScript code to detect whether the Enter key is pressed: const input = document. querySelector("input"); input. addEventListener("keyup", (event) => { if (event.

What is input keypress?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .


1 Answers

This I think will do the job, or if not is very close and will need only minor tweaking. The thing you have to remember is that you can't reliably tell anything at all about any character that may be typed in a keydown or keyup event: that all has to be done in a keypress handler. The definitive resource for key events is http://unixpapa.com/js/key.html

You also need to consider pastes, which this code won't handle. You will need to have separate paste event handler (although this event isn't supported in Firefox < 3.0, Opera, and very old WebKit browsers). You'll need a timer in your paste handler since it's impossible in JavaScript to access the content that's about to be pasted.

function isCharacterKeyPress(evt) {     if (typeof evt.which == "undefined") {         // This is IE, which only fires keypress events for printable keys         return true;     } else if (typeof evt.which == "number" && evt.which > 0) {         // In other browsers except old versions of WebKit, evt.which is         // only greater than zero if the keypress is a printable key.         // We need to filter out backspace and ctrl/alt/meta key combinations         return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8;     }     return false; }  <input type="text" onkeypress="alert(isCharacterKeyPress(event))"> 
like image 166
Tim Down Avatar answered Oct 07 '22 17:10

Tim Down