Input device event handling fully reimplemented, more robust this time