Implement most of keyboard input handling.