How to find out what character key is pressed?
260,308
Solution 1
"Clear" JavaScript:
function myKeyPress(e){
var keynum;
if(window.event) { // IE
keynum = e.keyCode;
} else if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
alert(String.fromCharCode(keynum));
}
<input type="text" onkeypress="return myKeyPress(event)" />
JQuery:
$("input").keypress(function(event){
alert(String.fromCharCode(event.which));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input/>
Solution 2
There are a million duplicates of this question on here, but here goes again anyway:
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
var charStr = String.fromCharCode(charCode);
alert(charStr);
};
The best reference on key events I've seen is http://unixpapa.com/js/key.html.
Solution 3
More recent and much cleaner: use event.key
. No more arbitrary number codes!
NOTE: The old properties (
.keyCode
and.which
) are Deprecated.
node.addEventListener('keydown', function(event) {
const key = event.key; // "a", "1", "Shift", etc.
});
If you want to make sure only single characters are entered, check key.length === 1
, or that it is one of the characters you expect.
Solution 4
Try:
<table>
<tr>
<td>Key:</td>
<td id="key"></td>
</tr>
<tr>
<td>Key Code:</td>
<td id="keyCode"></td>
</tr>
<tr>
<td>Event Code:</td>
<td id="eventCode"></td>
</tr>
</table>
<script type="text/javascript">
window.addEventListener("keydown", function(e) {
//tested in IE/Chrome/Firefox
document.getElementById("key").innerHTML = e.key;
document.getElementById("keyCode").innerHTML = e.keyCode;
document.getElementById("eventCode").innerHTML = e.code;
})
</script>
*Note: this works in "Run code snippet"
This website does the same as my code above: Keycode.info
Solution 5
Use this one:
function onKeyPress(evt){
evt = (evt) ? evt : (window.event) ? event : null;
if (evt)
{
var charCode = (evt.charCode) ? evt.charCode :((evt.keyCode) ? evt.keyCode :((evt.which) ? evt.which : 0));
if (charCode == 13)
alert('User pressed Enter');
}
}
Author by
xXx
Updated on July 08, 2022Comments
-
xXx almost 2 years
I would like to find out what character key is pressed in a cross-browser compatible way in pure Javascript.
-
VoVaVc over 10 yearsthis works ok for alphabetic chars, but what about dots/brakets and other typogtaphic symbols?
-
Tim Down about 9 years@VoVaVc: It works for those too. The crucial thing is using the
keypress
event, which gives you a character code, rather thankeyup
orkeydown
which give you a key code. -
Chris F Carroll almost 9 yearsIsn't this a near-exact duplicate of @Coyod's 2009 answer?
-
aljgom about 7 years
e.which
is deprecated. Usee.key
instead, as inif(e.key == 'z')
-
Kelderic over 6 years@aljgom,
e.key
still doesn't have full browser support. -
Curtis over 5 yearsDoesn't work for symbols that you have to press shift to make, like !
-
Ray over 5 years@AndyMercer key is now supported by all major browsers: developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/…
-
Zuabi about 4 yearsThe keypress event seems to be deprecated w3.org/TR/DOM-Level-3-Events/#event-type-keypress
-
merrybot almost 4 yearsIs this for node.js?
-
Gibolt almost 4 yearsNo, node just means any DOM element. If you had Node.js connecting to a UI, I suppose it would work
-
Irrmich almost 4 yearsUncaught TypeError: Mousetrap.record is not a function at recordSequence (****.html:12) at HTMLButtonElement.onclick (****.html:20)
-
Angelotti over 3 yearskeydown for some mobile device doesn't work it return undefine
-
Danziger over 3 yearsHere's another similar site but with a few additional options: keyjs.dev
-
Paolo Benvenuto over 2 yearson chrome, xfce, italian keyboard, spanish layout, pressing "<" returns "/", which is the key which is there in english keyboard. Why?!? some chrome versions ago it was not that!
-
MindRoasterMir over 2 yearstwo negative votes but now explaination why it is voted negative. I think it help when you comment what is the problem with this answer. thanks
-
gomozor over 2 yearseasy to vote, I guess you bettter try this snippet on your own :) title is pretty common by the way