admin管理员组文章数量:1386669
I have written a piece of javascript code to get the key pressed within a text area. I have used the onkeydown event to capture the key pressed and am calling a function when the event is triggered. Within the function i am using event.which to get the key pressed. But this is not giving the correct key pressed. For any character pressed, it gives the Ascii value of the corresponding upper case character ( 65 to 90 only ). It is not giving Ascii values for the lower case characters, ie 97 to 122, even if a lower case character has been typed. Eg - If i type 'a' it gives the Ascii value of 'A' Does any one know why this is happening ? Here is the code i am using -
var mainDoc = document.getElementById("mainDoc");
mainDoc.onkeydown = function(event){keyPress(event);}
function keyPress(event)
{
alert("key code : "+ event.which + " );
}
I tried using onkeypress event. This seems to work fine, but it does not capture the alt, control, arrow keys etc.
I have written a piece of javascript code to get the key pressed within a text area. I have used the onkeydown event to capture the key pressed and am calling a function when the event is triggered. Within the function i am using event.which to get the key pressed. But this is not giving the correct key pressed. For any character pressed, it gives the Ascii value of the corresponding upper case character ( 65 to 90 only ). It is not giving Ascii values for the lower case characters, ie 97 to 122, even if a lower case character has been typed. Eg - If i type 'a' it gives the Ascii value of 'A' Does any one know why this is happening ? Here is the code i am using -
var mainDoc = document.getElementById("mainDoc");
mainDoc.onkeydown = function(event){keyPress(event);}
function keyPress(event)
{
alert("key code : "+ event.which + " );
}
I tried using onkeypress event. This seems to work fine, but it does not capture the alt, control, arrow keys etc.
Share Improve this question asked Mar 28, 2012 at 10:02 Code maniacCode maniac 861 silver badge4 bronze badges4 Answers
Reset to default 6keydown
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./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 puter'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.
本文标签: Issue with javascript onkeydowneventwhich give characters in upper case onlyStack Overflow
版权声明:本文标题:Issue with javascript onkeydown - event.which give characters in upper case only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744521023a2610454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论