Fixing the event loop for hanlding keyevents