Keyboard events and main loop.