KeyboardEvent keyCode Property

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP







<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->




KeyboardEvent keyCode Property



❮ DOM Events
❮ KeyboardEvent




Example


Get the Unicode value of the pressed keyboard key:



var x = event.keyCode;

Try it Yourself »

More "Try it Yourself" examples below.



Definition and Usage


The keyCode property returns the Unicode character code of the key that
triggered the onkeypress event, or the
Unicode key code of the key that triggered the onkeydown or
onkeyup event.


The difference between the two code types:


  • Character codes - A number which represents an ASCII character

  • Key codes - A number which represents an actual key on the keyboard

These types do not always mean the same thing; for example, a lower case
"w" and an upper case "W" have the same keyboard code, because the key that is
pressed on the keyboard is the same (just "W" = the number "87"), but a different character code
because the resulting character is different (either "w" or "W",
which is "119" or "87") - See "More
Examples" below to better understand it.


Tip: To find out if the user is pressing a printable key (e.g.
"a" or "5"), it is recommended to use this property on the onkeypress event. To
find out if the user is pressing a function key (e.g. "F1", "CAPS
LOCK" or "Home") use the onkeydown or onkeyup event.


Note: In Firefox, the keyCode property does not work on the
onkeypress event (will only return 0). For a cross-browser solution, use the
which property together with keyCode, e.g:




var x = event.which || event.keyCode;  // Use either which or keyCode, depending on browser support


Tip: For a list of all Unicode characters, please study our
Complete Unicode Reference.


Tip: If you want to convert the returned Unicode value into a
character, use the fromCharCode() method.


Note: This property is read-only.


Note: Both the keyCode
and which property is provided for compatibility
only. The latest version of the DOM Events Specification recommend using the
key property instead (if available).


Tip: If you want to find out whether the "ALT", "CTRL",
"META" or "SHIFT" key was pressed when a key event occured, use the
altKey,
ctrlKey, metaKey or
shiftKey property.



Browser Support














Property
keyCode Yes Yes Yes Yes Yes




<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->




Syntax



event.keyCode

Technical Details






Return Value:
A Number, representing either a Unicode character code or the Unicode key code
DOM Version: DOM Level 2 Events

More Examples



Example


Using onkeypress and onkeydown to demonstrate the differences between
character codes and keyboard codes:



<input type="text" onkeypress="uniCharCode(event)" onkeydown="uniKeyCode(event)">


function uniCharCode(event) event.keyCode;
  document.getElementById("demo").innerHTML = "Unicode CHARACTER code: " + char;


function uniKeyCode(event)
  var key = event.keyCode;
  document.getElementById("demo2").innerHTML = "Unicode KEY code: " + key;


When pressing the "a" key on the keyboard (not using caps lock), the result of char and key will be:


Unicode CHARACTER code: 97
Unicode KEY code: 65


Try it Yourself »



Example


Alert some text if the user presses the Escape key:



<input type="text" onkeydown="myFunction(event)">

function myFunction(event)
  var x = event.keyCode;
  if (x == 27)   // 27 is the ESC key
    alert ("You pressed the Escape key!");

  


Try it Yourself »



Example


Convert the Unicode value into a character (does not work for function keys):



var x = event.keyCode;                // Get the Unicode value

var y = String.fromCharCode(x);       // Convert the value into a character

Try it Yourself »


Related Pages


HTML DOM reference: KeyboardEvent key Property


HTML DOM reference: KeyboardEvent charCode Property


HTML DOM reference: KeyboardEvent which Property




❮ DOM Events
❮ KeyboardEvent


Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế