Send proper key codes. Add option to auto-detect serial port

And some refactoring and bugfixing.