Keyboard handler rewritten