Improved the keyinput function