代码之家  ›  专栏  ›  技术社区  ›  Greg Domjan

如何使Java.awt.Robot类型为unicode字符?(可能吗?)

  •  9
  • Greg Domjan  · 技术社区  · 16 年前

    我们有一个用户提供的字符串,可能包含unicode字符,我们希望机器人键入该字符串。

    如何将字符串转换为机器人将使用的键码?
    您是如何做到这一点的,这样它也是独立于java版本的(1.3->1.6)?

    我们为“ascii”字符工作的是

    //char c = nextChar();
    //char c = 'a'; // this works, and so does 'A'
    char c = 'á'; // this doesn't, and neither does 'Ă'
    Robot robot = new Robot();
    KeyStroke key = KeyStroke.getKeyStroke("pressed " + Character.toUpperCase(c) );
    if( null != key ) {
      // should only have to worry about case with standard characters
      if (Character.isUpperCase(c))
      {
        robot.keyPress(KeyEvent.VK_SHIFT);
      }
    
      robot.keyPress(key.getKeyCode());
      robot.keyRelease(key.getKeyCode());
    
      if (Character.isUpperCase(c))
      {
        robot.keyRelease(KeyEvent.VK_SHIFT);
      }
    }
    
    5 回复  |  直到 16 年前
        1
  •  11
  •   Wolfgang Steiner    14 年前

    基于javamonkey79的代码,我创建了以下代码段,它应该适用于所有Unicode值。。。

    public static void pressUnicode(Robot r, int key_code)
    {
        r.keyPress(KeyEvent.VK_ALT);
    
        for(int i = 3; i >= 0; --i)
        {
            // extracts a single decade of the key-code and adds
            // an offset to get the required VK_NUMPAD key-code
            int numpad_kc = key_code / (int) (Math.pow(10, i)) % 10 + KeyEvent.VK_NUMPAD0;
    
            r.keyPress(numpad_kc);
            r.keyRelease(numpad_kc);
        }
    
        r.keyRelease(KeyEvent.VK_ALT);
    }
    

        2
  •  3
  •   javamonkey79    16 年前

    这个 KeyEvent Class JRE 1.5中的许多unicode类没有直接映射。如果您在Windows机箱上运行此操作,您可能需要编写一个自定义处理程序,执行以下操作:

    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、 找出它们的键组合是什么,然后将它们映射到某种对象(可能是HashMap?),以便以后查找和执行。

        3
  •  0
  •   hecvd    15 年前

    我想这有点晚了,但是。。。

    Robot robot = new Robot();
    
       robot.keyPress( KeyEvent.VK_DEAD_ACUTE);
    
       robot.keyPress( KeyEvent.VK_A );
       robot.keyRelease( KeyEvent.VK_A );
    
       robot.keyRelease( KeyEvent.VK_DEAD_ACUTE );
    

    只需键入一个“”

        4
  •  -1
  •   Vladimir Bosyi    13 年前

    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 \