Optimise get_key_state by caching the modifier map and the state (this can probably still be improved in the future). Only call time(NULL) for the events where it's needed.