Unlike other key events in javascript, keypress
may capture the actual character, that would be inserted in text field (only applies on editable elements, like text fields and content-editable html elements). This is hidint under event.charCode
. On other events, such as keyup
and keydown
you only may capture ID of the key - with different systems, and languages, the same number may mean totally different characters.
On the other side, keypress
along with keyup
may not block many default event actions - like pressing tab, F5 or arrows.
Questions tagged [onkeypress]
377 questions
1449
votes
31 answers
Trigger a button click with JavaScript on the Enter key in a text box
I have one text input and one button (see below). How can I use JavaScript to trigger the button's click event when the Enter key is pressed inside the text box?
There is already a different submit button on my current page, so I can't simply make…

kdenney
- 18,343
- 4
- 31
- 27
332
votes
12 answers
How to handle the `onKeyPress` event in ReactJS?
How can I make the onKeyPress event work in ReactJS? It should alert when enter (keyCode=13) is pressed.
var Test = React.createClass({
add: function(event){
if(event.keyCode == 13){
alert('Adding....');
}
},
…

user544079
- 16,109
- 42
- 115
- 171
225
votes
10 answers
Android - How To Override the "Back" button so it doesn't Finish() my Activity?
I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar.
This is so that when the User presses home and the Activity gets pushed to the background they can get back to the Activity via…

Donal Rafferty
- 19,707
- 39
- 114
- 191
106
votes
6 answers
How do I detect keypresses in Javascript?
After looking around on the internet, I've seen a lot of suggestions (use window.onkeypress, use jQuery, etc.) but for almost every option there's a counterargument. How can I detect a keypress in Javascript?

bobismijnnaam
- 1,361
- 2
- 10
- 20
103
votes
12 answers
How to get text of an input text box during onKeyPress?
I am trying to get the text in a text box as the user types in it (jsfiddle playground):
function edValueKeyPress() {
var edValue = document.getElementById("edValue");
var s = edValue.value;
var lblValue =…

Ian Boyd
- 246,734
- 253
- 869
- 1,219
69
votes
11 answers
How to capture Enter key press?
In my HTML page, I had a textbox for user to input keyword for searching. When they click the search button, the JavaScript function will generate a URL and run in new window.
The JavaScript function work properly when the user clicks the search…

Joe Yan
- 2,025
- 8
- 43
- 62
41
votes
5 answers
jQuery keypress left/right navigation
I want to give my content slider the ability to respond to keypress (LEFT ARROW key and RIGHT ARROW key) feature. I have read about some conflicts between several browsers and operation systems.
The user can navigate the content while he is on the…

Tomkay
- 5,120
- 21
- 60
- 92
32
votes
3 answers
How do I disable Firefox's "Search for text when I start typing" on pages with keyboard shortcuts?
Some web pages such as GMail and Reddit(with the Reddit Enhancement Suite) have useful keyboard shortcuts that I'd like to use. However, whenever I start typing on one of these pages, the first onkeypress event fires, but then the "Search for text…

BinarySplit
- 538
- 1
- 4
- 8
32
votes
6 answers
keyPressEvent.getCharCode() returning 0 for all special keys like enter, tab, escape, etc
My code:
@Override
public void onKeyPress(KeyPressEvent event)
{
if (event.getCharCode() == KeyCodes.KEY_ENTER)
{
registerButton.click();
}
}
This is attached to a TextBox, and it does fire when I press enter. …

Riley Lark
- 20,660
- 15
- 80
- 128
24
votes
6 answers
onKeyListener not working on virtual keyboard
I don't understand why this piece of code is not working. Only backspace and return key are detected. Listener doesn't fire for any other key. My device is Nexus One.
I tried to override activity's OnKeyDown method and that's even worse. The only…

bobetko
- 5,019
- 14
- 58
- 85
17
votes
3 answers
Javascript Last character missing on OnKeyPress event
function h(x)
{
alert(x);
}
When I press 'a' alert empty
When I press 'b' after 'a' =>ab alert only 'a' and I want 'ab'
When I type 'abcd' it alert 'abc' only and I want 'abcd'

Wasim A.
- 9,660
- 22
- 90
- 120
12
votes
5 answers
C++: execute a while loop until a key is pressed e.g. Esc?
Does anyone have a snippet of code that doesn't use windows.h to check for a key press within a while loop. Basically this code but without having to use windows.h to do it. I want to use it on Linux and Windows.
#include
#include…

pandoragami
- 5,387
- 15
- 68
- 116
11
votes
5 answers
How to use Key Press Event on TextFormField in Flutter?
Is there any way to catch a keypress in textfield? In my case, when the user press enter key inside the text field, the values will be stored. For this to happen, I need to use Keypress-event like in Kotlin+Android. I just started trying flutter…

kinesis
- 373
- 1
- 4
- 15
11
votes
6 answers
How can I check the new value of a text field on keypress?
I have a single form field and I need to be able to detect when the field changes and execute some additional code using the new value of the field. I only want to execute the code if the key pushed actually changes the value of the text box. Here's…

Stephen Sorensen
- 11,455
- 13
- 33
- 46
11
votes
6 answers
Javascript Function to enter only alphabets on keypress
I want to enter only character values inside a

Lucy
- 1,812
- 15
- 55
- 93