How to make the Java.awt.Robot type unicode characters? (Is it possible?)

前端 未结 4 764
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 10:09

We have a user provided string that may contain unicode characters, and we want the robot to type that string.

How do you convert a string into keyCodes that the rob

4条回答
  •  暖寄归人
    2020-11-29 11:05

    The KeyEvent Class does not have direct mappings for many unicode classes in JRE 1.5. If you are running this on a Windows box what you may have to do is write a custom handler that does something like this:

    Robot robot = new Robot();
    char curChar = 'Ã';
    
    // -- isUnicode( char ) should be pretty easy to figure out
    if ( isUnicode( curChar ) ) {
       // -- this is an example, exact key combinations will vary
       robot.keyPress( KeyEvent.VK_ALT );
    
       robot.keyPress( KeyEvent.VK_NUMBER_SIGN );
       robot.keyRelease( KeyEvent.VK_NUMBER_SIGN );
    
       // -- have to apply some logic to know what sequence
       robot.keyPress( KeyEvent.VK_0 );
       robot.keyRelease( KeyEvent.VK_0 );
       robot.keyPress( KeyEvent.VK_1 );
       robot.keyRelease( KeyEvent.VK_1 );
       robot.keyPress( KeyEvent.VK_9 );
       robot.keyRelease( KeyEvent.VK_9 );
       robot.keyPress( KeyEvent.VK_5 );
       robot.keyRelease( KeyEvent.VK_5 );
    
       robot.keyRelease( KeyEvent.VK_ALT );
    }
    

    e.g. Figure out what they key combinations are, and then map them to some sort of Object (maybe a HashMap?) for later lookup and execution.

    Hope this helps :)

提交回复
热议问题