How to find the key code for a specific key

孤街醉人 提交于 2019-11-30 02:22:18
Bertrand Marron

    $(function () {
      $(document).keyup(function (e) {
         console.log(e.keyCode);
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here's your online tool.

Ahmet Kakıcı

Just googled and result

function displayunicode(e) {
  var unicode = e.keyCode ? e.keyCode : e.charCode;
  console.log(unicode);
}
<form>
  <input type="text" size="2" maxlength="1" onkeyup="displayunicode(event); this.select()" />
</form>
sri_bb

As in, what keyboard events reports based on what keys are pressed

  $("#textinput").keydown(function(e) {
    e.keyCode; // this value
  });

Try Here for all the key Events and These are the mobile key Events

The bottom of http://www.quirksmode.org/js/keys.html can show the keycode of keys you have pressed for the selected keyboard events.

Try this:

$('#myelement').keydown(function(event) {
  var code = event.keyCode;
  alert(code);
}
alsuren

Try not to hard-code too many keycodes. Let the JS library convert them for you wherever possible:

var code = ev.keyCode,
    ascii = String.fromCharCode(code);
alert(ascii);

Please try this.

$('input').on('keyup', function(e){
   var key_code = e.which || e.keyCode;
   console.log(key_code );
});

For better help please follow the below link http://www.w3schools.com/jsref/event_key_keycode.asp

If you are only looking for keyCode you essentially don't need to get the keypress event, you can simply convert character to keyCode and vise versa:

Char to KeyCode, for instance A ("A").charCodeAt(0) returns 65. Here's the syntax.

If you already know the characters which their keycodes are needed, say 'ABCDEFGH', you only need a loop to get all key codes:

var text = "ABCDEFGH";
for (var i=0; i< text.length; i++){
	console.log(text[i] ,text.charCodeAt(i))
}

It's obvious that this method is not going to be used for obtaining key codes of shif, ctrl or Alt key in keyboard, if you need them stick with the method stated above which uses keypress event.

FYI, to convert keyCode to Char: String.fromCharCode(65) returns A.

Vanilla javascript + Alert:

document.addEventListener('keypress', function(e) {
  alert("Key: " + e.code + ", Code: " + e.charCode)
});

Vanilla javascript + console:

document.addEventListener('keypress', function(e) {
  console.log("Key: " + e.code + ", Code: " + e.charCode)
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!