So I've got this textarea which listens for Enter keypress event. But I only want it to respond to the Enter key being pressed on the number pad not the one next to the letters.
What's a method I can use?
Both keys trigger the exactly same events with the same key code (13). It's not possible to differ between them in JavaScript.
There is no different keyCode for nomal enter and enter on keypad. Its 13 in both cases
http://unixpapa.com/js/key.html
They actually do the same thing and are indistinguishable
Related
I'm working on #mention feature and need to load list of users that start with entered character in an input text field .
The problem is with Spanish character like (íáúóé).
in my code I use keypress and keydown to detect characters but in Spanish the keypress event doesn't fire .
in order to type the "á" user need to press two keys
first one is "´" and second is "a" ,then we got "á".
in keydown event using the method String.fromCharCode(e.which || e.keyCode) on this key"´" return "å"
The question how to catch this scenario?
Thanks
I have been trying to deal with getting the last input in to a text field on an Android app from Javascript.
I was originally trying to use the KeyUp, KeyDown and KeyPress events to get the KeyCodes from keyboard input however with android soft keyboards you will get the KeyCode 229 for every key other than backspace, space and enter.
So now I am using the onInput event however this just seems to alert me to the fact that something has been input in to a text field and not what that input was.
document.getElementById("textarea").addEventListener("input", function (e) {
ClaroSpeak.KeyHandler(e);
}, false);
Does the input event some how let me know what the last change or key was, e.g. 'space' 'a' 'F' ect...
What about setting a 'global' variable and storing the last element in it?
I have an input box and want to adjust the value when a key is pressed.
'keypress .comment-input': 'onCommentInputKeyBlur',
When a key is pressed, a class is added to the html element to reflect the changes.
onCommentInputKeyBlur: function(ev) {
var $form = $('#comment-submit');
if (ev.which) {
$form.addClass('focused');
} else if (!$(ev.currentTarget).val()) {
$form.removeClass('focused');
}
},
However, this doesn't detect special keys being pressed (ie when a user presses ctrl+v for a paste, it's not detected and the formatting is therefore wrong).
Using keyup and keydown halfway solve the problem, but formatting is wrong for a brief second:
Keydown -> the value isn't supposed to change until an actual value is entered through a keypress, but it changes right when ctrl is hit instead of waiting for the second key
Keyup -> new value is briefly pasted over the previous one while waiting for the key to actually be released.
Is there a better way to about solving this? Ideally, I would like to detect if the key entered does actually produce a value and is not simply a special key.
Look into the onchange event. It is fairly well supported and may solve your problem.
What is the ultimate goal of this functionality? perhaps there is a better approach.
If you insist on doing this via keypress event, then you can simply check event.keyCode and filter out the few keycodes that dont change anything, such as shift, control, alt.
I have a question regarding the char codes for keyboards. I was reading this article
This has a bunch of key codes.I want to check what key code to use when user presses the alt and down arrow.I have to create dropdowns like comboboxes which displays the list when user presses alt and down key.
Thanks
The keyboard event has event.altKey to indicate whether alt was pressed.
Read https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent for more information.
The easiest way to find out any key code is to put console.log(event.data.keyCode) into your event function.
For example key code for "Alt + Down" is 4456488.
$("#epFilter").keypress(function(){
query = $("#epFilter").val();
alert(query);
});
In this code, whenever I type in my text box, it always alert one character less. It doesn't catch the last character. Why?
Use the keyup event instead.
$("#epFilter").keyup(function(){
query = $("#epFilter").val();
alert(query);
});
keypress event is triggered when user presses a key, but before the character is inserted. Use keyup event instead.
Slightly improving Matt's answer (using this inside event handler):
$("#epFilter").keyup(function(){
query = $(this).val();
alert(query);
});
I wonder if that is a "normal" behavior. Shouldn't keypress be similar to keydown+keyup (except for the codes and keys)?
I had the same problem. I have an input-text field and I want to calculate a total on keypress. However, it does not change the total until I input again another character (this means when I entered the first digit it didn't update). I used "keyup" and was "fixed", but I think it could be better if I use keypress as it doesn't fire when I press other keys (like Ctrl, Shift, arrows, etc).
According to the documentation Keypress should work as I expected but for some reason it didn't.
This is not exactly an answer (so don't vote for it). Just my contribution.