最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Keycode for a: 65 or 97? - Stack Overflow

programmeradmin5浏览0评论

I'm working with javascript (on a macbook pro OSX 10.11.x, not sure if this matters) using Chrome browser. Im using the function:

window.onkeypress = function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("keypressed = " + key);
}

when i press 'a' key on my keyboard, it logs as 97, however this does not correspond to any other keyCode list i find on the internet, which states 'a' is 65.

This is the same for other keys as well, for example, 's' for me is 115, but everyone else states that 's' is 83.

Is there a dependency that i'm missing? If i fire an event assuming a == 95, will it work on other browsers?

Thanks.

I'm working with javascript (on a macbook pro OSX 10.11.x, not sure if this matters) using Chrome browser. Im using the function:

window.onkeypress = function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("keypressed = " + key);
}

when i press 'a' key on my keyboard, it logs as 97, however this does not correspond to any other keyCode list i find on the internet, which states 'a' is 65.

This is the same for other keys as well, for example, 's' for me is 115, but everyone else states that 's' is 83.

Is there a dependency that i'm missing? If i fire an event assuming a == 95, will it work on other browsers?

Thanks.

Share Improve this question asked Jul 27, 2017 at 21:07 noobcodernoobcoder 3431 gold badge3 silver badges13 bronze badges 4
  • 4 The "keypress" event doesn't give you key codes, it gives you character codes. – Pointy Commented Jul 27, 2017 at 21:10
  • character (chr) code asciitable.com – CrayonViolent Commented Jul 27, 2017 at 21:10
  • 2 a = 97 and A = 65 – PRMoureu Commented Jul 27, 2017 at 21:11
  • @Pointy Thank you, because the function was .keyCode(), i went ahead and searched up javascript key code. And i realized now that lowercase a is indeed 97, upper case is 65. Thank you! – noobcoder Commented Jul 27, 2017 at 21:14
Add a comment  | 

2 Answers 2

Reset to default 11

So I found that a capital A is indeed, 65.

A lowercase a is 97

Please see this chart:

Chart original location: http://www.asciitable.com/

Capital letters are not the same as lower-case and produce different codes.

Also, the keypress event works differently than the keyup or keydown events. keypress responds to printable characters and gives the code of the character that was produced. With keyup and keydown, the code represents the physical hardware key on the keyboard that was pressed. For example, if you run the snippet below and just press the SHIFT key, you will not see the keypress event log message at all because that event doesn't fire for that key.

window.addEventListener("keyup", function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("key up = " + key, e.key);
});

window.addEventListener("keydown", function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("key down = " + key, e.key);
});

window.addEventListener("keypress", function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("key pressed = " + key, e.key);
});
Just click in this area to give it the focus, then press some keys.

发布评论

评论列表(0)

  1. 暂无评论