Issue with javascript onkeydown - event.which give characters in upper case only

南楼画角 提交于 2019-12-01 06:59:39

keydown and keyup don't (mostly) give you characters at all, they give you keycodes. keypress is where you get characters (and if you need to know, also the state of the modifier keys as of when that character was typed, on the event object's ctrlKey, altKey, shiftKey, and metaKey properties). This page goes into the madness that is keyboard events in JavaScript in loving detail...

The issue is that keydown event will report keypresses - that is, physical keys pressed. The keypress event will report the translated keys, meaning the character that has been derived from the keys pressed (Shift + a == A). In order to get the actual keys, you may need to keep track of both keydown/keyup (to monitor the modifier keys) and keypress (to monitor the actual alphanumeric user input.)

From http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key-Events.aspx

In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.

function getKeyCode(event) {
   event = event || window.event;
   return event.keyCode;
}

use event.keyCode will give u the keycode ;)

g.r.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!