Refactoring and implementation changes to keyboard and mouse handling

See issue #505
11 files changed