Improved mouse support

Made mouse-to-keyboard transitions more natural, and fixed an issue
with cursor bounds checking.