Race condition in keyboard code?