views:

474

answers:

1

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

For example if I type a lowercase 'a', the window.event.keycode is 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'

+1  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

James

related questions