0

We are assigning new key codes to an onKeyPress event in IE with this code:

window.event.keyCode=65

In IE, this changes the key received by other event handlers in the chain, but this code doesn't work in chrome. Is there any way to do this?

Example HTML:

<input type="text" name="hotelfloornew" id="hotelfloornew" 
       onkeypress="uPPer();" size="25" />

Corresponding JS:

function uPPer()
{
    switch (window.event.keyCode)
    {       
        case 97:window.event.keyCode=65;break;
        case 98:window.event.keyCode=66;break;  
        case 99:window.event.keyCode=67;break;  
        case 100:window.event.keyCode=68;break;
.
.
.

    }
}
Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
MC_delta_T
  • 596
  • 1
  • 9
  • 22
  • Possible duplicate of [change KeyCode in javascript in google chrome](http://stackoverflow.com/questions/15404807/change-keycode-in-javascript-in-google-chrome) – KyleMit Oct 15 '15 at 22:35

3 Answers3

3

IE assigns a global event value, but that's not normalized. In other browsers, use the value provided to your callback :

window.onkeypress = function(event) {
   console.log(event.keyCode);
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

Changing the keyCode property of an event is a grotty hack that only works in old IE. It should be read-only and is in all sensible browsers. Don't do it.

If you need to change the effect of a keypress, prevent its default action and do your own replacement action manually. If you want to map typed characters to other characters, you can use code I've published before on Stack Overflow.

Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536
0
function doKey(e) {
        evt = e || window.event; // compliant with ie6        
        alert(evt.keyCode+' Pressed');
    }

window.event.keyCode is mostly for IE... Here is an exemple code that will alert the keyCode pressed in all navigators.

Salketer
  • 14,263
  • 2
  • 30
  • 58