How about removing lines 571, 572 and 575, and putting it all here:

revertTo = ' ' + String.fromCharCode(revertToKeycode || keycode);

I think that would be cleaner because it would do all of the correction code first without cluttering it up with revert code.