44

How can I convert a character to its respective keycode?

For example:

  • a to 65
  • b to 66
  • c to 67
  • d to 68
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Santhosh
  • 19,616
  • 22
  • 63
  • 74
  • see [http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed](http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed) – goldenratio Sep 16 '09 at 04:50
  • see [FULL list of JavaScript keycodes](http://stackoverflow.com/questions/5603195/full-list-of-javascript-keycodes) – Janus Troelsen May 01 '13 at 01:12

7 Answers7

43

You can use the charCodeAt function to achieve this.

Working example:

function showKeyCode () {
    var character = document.getElementById("character").value.substring(0, 1);
    var code = document.getElementById("character").value.charCodeAt(0);
    var msg = "The Key Code for the \"" + character + "\" character is " + code + ".";
    alert(msg);
}
<input type="text" id="character" size="15">
<input type="button" value="Show Key Code" onclick="showKeyCode();">
Ethan
  • 4,295
  • 4
  • 25
  • 44
rahul
  • 184,426
  • 49
  • 232
  • 263
  • i'm having a char let say 'a' and i need to find the respective keycode of 'a'. – Santhosh Sep 16 '09 at 04:50
  • 45
    I'm not sure whether this really answers the question - it depends what you mean by "keycode". This function gives you the character code, not the key code, which may not be the same thing. – Tim Down Sep 23 '09 at 14:52
  • 6
    Either the term keycode MUST be changed in the original question, or this is a wrong answer. Keycode is what you get from e.which - when a key is pressed. – Simon Steinberger Jun 22 '14 at 18:56
  • 3
    Something is wrong.... when I press "." (the point) on my keyboard my event.KeyCode = 190.... If I do your code the ".".charCodeAt(0) = 46... why? – Simone May 05 '15 at 08:12
  • This doesn't answer the question as it's currently worded. If you're actually looking to convert ASCII characters to keycodes, see Tim's answer http://stackoverflow.com/a/1432067/78639 – Jan Sep 09 '15 at 01:05
  • This is not the correct/authoritative answer for the question, the solution provided does not return the keycode for the character pressed on the keyboard. There are several references here that point to that. – 3coins May 17 '16 at 01:35
  • some of code will work, others dont ... http://jsbin.com/rovogiqucu/edit?html,js,output – Benoit Oct 06 '16 at 20:50
  • `charCodeAt` doesn't work for `'a'`, it gives 97 but the keycode is 65 – Omu Oct 08 '17 at 10:14
14

What do you mean by "keyCode"? Different browsers have different keyCode values in keyup and keydown events which will not necessarily correspond to the ASCII code for the corresponding character. For alphanumeric keys, the keypress event will give you the ASCII code in most browsers via the charCode or which properties. This page is useful.

Update September 2015

As pointed out by Jan in the comments, keyCode will eventually be superseded by the superior key property. However, there is not much browser support for this yet.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • @Jan: According to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent), `char` has been dropped. `key` is much better than `keyCode` but is not yet widely supported. For the short-to-medium term, `keyCode` and `which` are staying with us. – Tim Down Sep 09 '15 at 11:18
  • Nice. Yours should be the accepted answer by the way. The accepted answer doesn't answer the question at all. It answers a *different* question, but not the asked one – Jan Sep 09 '15 at 17:40
  • 2
    "the superior key property"?!? I don't see how working with strings like `"ArrowDown"` or `"Enter"` is supposed to be better than working with ASCII Integer values or how deprecating both `keyCode` and `which` is supposed to be helpful to anyone. Without at least a practical, standard way to convert `"Enter"` to `13` and back, this makes no sense whatsoever! – John Slegers Aug 24 '17 at 11:20
  • As of 2021, `key` does seem to have [decent support](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#browser_compatibility). – Codesmith Aug 17 '21 at 14:11
9

Here is a object with some keys and their corresponding keycodes:

{"0":48,"1":49,"2":50,"3":51,"4":52,"5":53,"6":54,"7":55,"8":56,"9":57,"d":68,"b":66,"a":65,"s":83,"i":73,"f":70,"k":75,"ß":219,"Dead":220,"+":187,"ü":186,"p":80,"o":79,"u":85,"z":90,"t":84,"r":82,"e":69,"w":87,"g":71,"h":72,"j":74,"l":76,"ö":192,"ä":222,"#":191,"y":89,"x":88,"c":67,"v":86,"n":78,"m":77,",":188,".":190,"-":189,"ArrowRight":39,"ArrowLeft":37,"ArrowUp":38,"ArrowDown":40,"PageDown":34,"Clear":12,"Home":36,"PageUp":33,"End":35,"Delete":46,"Insert":45,"Control":17,"AltGraph":18,"Meta":92,"Alt":18,"Shift":16,"CapsLock":20,"Tab":9,"Escape":27,"F1":112,"F2":113,";":188,":":190,"_":189,"'":191,"*":187,"Q":81,"W":87,"E":69,"R":82,"T":84,"Z":90,"S":83,"A":65,"D":68,"I":73,"U":85,"O":79,"Y":89,"X":88,"C":67,"F":70,"V":86,"G":71,"B":66,"H":72,"N":78,"J":74,"M":77,"K":75,"L":76,"P":80,"Ö":192,"Ä":222,"Ü":186,"!":49,"\"":50,"§":51,"$":52,"%":53,"&":54,"/":55,"(":56,")":57,"=":48,"?":219,"°":220}

if some keys are missing or are different for your browser, you can use the following function:

let a = {}
document.addEventListener("keydown", ({key, keyCode}) => a[key] = keyCode);

Just copy it to the console and smash your head on the keyboard. The result will be stored in a

Teiem
  • 1,329
  • 15
  • 32
5

As pointed out in the comments: the accepted answer is not correct, because it gives the character code and not the keyCode, which can be different, e.g. 'a'.charCodeAt(0) == 97 while the correct keyCode is 65.

for converting just the standard characters from [a-zA-Z] or numbers [0-9] the code below can be used.

However this does not work correctly for any special keys like ., Ö, # or whatsoever and I did not find a good solution for them. As a workaround one could use a site like http://keycode.info/ (which just captures the onkeydown events and reads the event.keyCode property).

function convertToKeyCode(target) {
    var keyCode = target.value.toUpperCase().charCodeAt(0);
    document.getElementById("keyCodeSpan").innerHTML = keyCode;
}
<input type="text" oninput="convertToKeyCode(this)" size="1" maxlength="1">
<span>Keycode: </span><span id="keyCodeSpan"></span>
klues
  • 847
  • 12
  • 21
2

Find a keycode/ascii chart like this one and put it into an array such that array['char'] = keycode. This is tedious, but the code will execute pretty fast.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
0

I was searching for this when I stumbled upon this question. I do not think the marked answer is the right answer. For simple letters, A-Za-z, I use the following:

function getKeyCode(char) {
  var keyCode = char.charCodeAt(0);
  if(keyCode > 90) {  // 90 is keyCode for 'z'
    return keyCode - 32;
  }
  return keyCode;
}

Please note that this is meant to work for characters A-Z and a-z only.

praty
  • 914
  • 9
  • 11
0

let character = prompt('input a character');

console.log(character.charCodeAt(0));

for basic concept,you should use the method 'string.charCodeAt(0)'. for Example in Javascript , 'A'.charCodeAt(0).

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 06 '21 at 16:40