如何使 Java.awt.Robot 类型的 unicode 字符?(是否可以?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/397113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:12:35  来源:igfitidea点击:

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

javaunicodeautomation

提问by Greg Domjan

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

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

How do you convert a string into keyCodes that the robot will use?
How do you do it so it is also java version independant (1.3 -> 1.6)?

如何将字符串转换为机器人将使用的 keyCode?
你是怎么做的,所以它也是java版本独立的(1.3 - > 1.6)?

What we have working for "ascii" chars is

我们为“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);
  }
}

采纳答案by Wolfgang Steiner

Based on javamonkey79's code I've created the following snippet which should work for all Unicode values...

基于 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);
}

This automatically goes through each decade of the unicode key-code, maps it to the corresponding VK_NUMPAD equivalent and presses/releases the keys accordingly.

这会自动遍历 unicode 键码的每个十年,将其映射到相应的 VK_NUMPAD 等效项,并相应地按下/释放键。

回答by javamonkey79

The KeyEvent Classdoes 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:

KeyEvent的类不具有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.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.

例如,弄清楚它们的键组合是什么,然后将它们映射到某种对象(可能是 HashMap?)以供以后查找和执行。

Hope this helps :)

希望这可以帮助 :)

回答by hecvd

i think this is a bit late but...

我觉得这有点晚了但是...

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 );

that just type an "á"

只需输入“á”

回答by Vladimir Bosyi

The best way that i find when solve simulare problem

我在解决simulare问题时找到的最好方法

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\

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