JavaScript > Others

KeyCode unicode value in JavaScript

How to get the Unicode value of the pressed keyboard key in JavaScript?


By using event.keycode property we can get the unicode of the pressed keyboard key. Read the complete list of unicode of all keys of the keyboard.

<p>Place the cursor inside the textbox and press any key of the keyboard to know the Unicode of the OnPressKey</p>
<input type="text" size="45" onkeypress="myEvent(event)">
<p id="myId"></p>

<script>
    function myEvent(event) {
        var r = event.which || event.keyCode; //* In some browsers keycode property does not work, in that case which property helps to get the output. So, we used both properties event.which OR event.keycode *// 
        document.getElementById("myId").innerHTML = "The Unicode of the OnPress Key is: " + r;
    }
</script>

In the above code snippet we have given Id as "myId" to the second <p> element in the HTML code. There is a function myEvent(event) in the script code which is connected to the onclick of the HTML button.

We need to get the unicode value of the pressed keyboard key, for that we are using event.keycode in the variable rvar r =event.which || event.keycode, this is because of keycode property does not work in all the browsers, where as which property does.

With the above code snippet you will get an output with empty textbox, place the mouse curser inside the textbox and press any key of the keyboard to get the unicode of the OnPress key of the keyaboard.

OUTPUT

NOTE: The above output is showing the unicode value of the character T, please refresh the page and try with new characters to know the uniocde of the OnPresskeys.

 Views: 8187 | Post Order: 197



Write for us






Hosting Recommendations