expanded keyboard event handling code