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

前端 未结 4 768
隐瞒了意图╮
隐瞒了意图╮ 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:13

    The best way that i find when solve simulare problem

    import java.awt.AWTException;
    import java.awt.Robot;
    
    public class MyRobot {
    
        public static void typeString(String s)
            {
                try {
                Robot robik = new Robot();
                byte[] bytes = s.getBytes();
                for (byte b : bytes)
                {
                    int code = b;
                    // keycode only handles [A-Z] (which is ASCII decimal [65-90])
                    if (code > 96 && code < 123) code = code - 32;
                    robik.delay(40);
                    robik.keyPress(code);
                    robik.keyRelease(code);
                }
            } catch (AWTException e){
    
        }
      }
    
    }
    

    http://www.devdaily.com/java/java-robot-class-example-mouse-keystroke\

提交回复
热议问题