开发者

Google Chrome KeyCode Assignment

I'm using javascript to make the keyboard input upper case.

For example if I type a lowercase 'a', the window.event.keycode i开发者_StackOverflow社区s 97.

Does any one know if the following works in Chrome. Works fine in IE

window.event.keycode = 97 - 32;

should be an upper case 'A'


Lowercase letters have the same key codes as uppercase letters. You'll have to use keydown in conjuction with keyup to tell if the user pressed shift to produce the letter. I recently wrote a script using jquery:

var shiftDown = false;
var outString;
$(document).ready(function() {
    $(document).keydown(function(event) {
       if (event.keyCode == 16)
       shiftDown = true;
    });
$(document).keyup(function(event){
     if (event.keyCode != 16)
     {
         if (shiftDown)
         {
             outString += (String.fromCharCode(event.keyCode)).toUpperCase();
         }
         else
         {
             outString += String.fromCharCode(event.keyCode);
         }
     }
     shiftDown = false;
});

Something interesting is that some times keycodes are different depending on the event. Here's a tester page I found very useful: http://asquare.net/javascript/tests/KeyCode.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜