How to convert a char to its keycode?

前端 未结 5 1367
闹比i
闹比i 2020-12-09 14:26

How can I convert a character to its respective keycode?

For example:

  • a to 65
  • b to 66
5条回答
  •  借酒劲吻你
    2020-12-09 15:22

    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.

提交回复
热议问题