Improved keyPress and keyDown event handling