Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the key code for a specific key

What's the easiest way to find the keycode for a specific key press?

Are there any good online tools that just capture any key event and show the code?

I want to try and find the key codes for special keys on a mobile device with a web browser, so an online tool would be great.

like image 528
Acorn Avatar asked Oct 29 '10 18:10

Acorn


2 Answers

    $(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.

like image 130
Bertrand Marron Avatar answered Oct 02 '22 08:10

Bertrand Marron


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.

like image 30
Muhammad Musavi Avatar answered Oct 02 '22 09:10

Muhammad Musavi