1 Reply Latest reply on Apr 15, 2010 12:25 PM by 843810

    Any way to map characters to sequences of virtual key codes?

      I have a text and I need to make the java.awt.Robot class instance to type it. The text may contain any Unicode characters. Unfortunately the Robot class accepts only virtual key codes which generate different characters depending on the currently used keyboard layout. I was wondering whether there is any way to create a map of how to convert Unicode characters onto combinations of keyboard layout(s) and sequences of KeyEvent virtual key codes?

      I was originally hoping to find access to the method which interprets sequences of virtual keys (represented by KeyEvent instances of type KEY_PRESSED and KEY_RELEASED) and generates a KEY_TYPED key event (which already contains the resulting character). I however failed to find the code responsible for this functionality and I'm afraid this is done by the underlying operating system. Can anyone give me a hint on where to search?

      The only workable solution I've found so far is to fire sequences of KEY_PRESS and KEY_RELEASED events and their combinations with the Shift key, listen for eventual KEY_TYPED events and save the mapping between the original press/release sequence and the resulting character if such an event is received. This can be repeated for a set of keyboard layouts switched through InputContext.selectInputMethod().

      This solution is however an ugly, inefficient and time consuming hack which may or may not find mapping for a particular character. It also requires a GUI displayed so that one can receive key events, and I also need to find a solution which would run without GUI just from CLI.

      Does anyone have an idea on how to implement this more efficiently?
        • 1. Re: Any way to map characters to sequences of virtual key codes?
          I suspect that you are trying to solve the wrong problem. I don't see any reason for converting characters to virtual keys because:
          * this is dependent of current keyboard layout
          * many characters will not map at all

          If you want to use shortcuts use the virtual key codes instead. I wrote few articles regarding keyboard usage in an international context - check http://blog.i18n.ro/category/readiness/input/