Improve key state handling and conversion of virtual keys to characters.