Can I change the keyCode property of an event in Chrome?

advertisements

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;
.
.
.

    }
}


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.