Improved mouse and key handling