使用 java.awt.Robot 输入字符串
Type a String using java.awt.Robot
我已经知道如何使用 java.awt.Robot
使用 keyPress
键入单个字符,如下所示。我如何才能将 whole 预定义的 String
值 一次 简单地输入文本框?
robot.keyPress(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_1);
// instead, enter String x = "111"
常见的解决方案是使用剪贴板:
String text = "Hello World";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
您需要 "type" 角色,这是一个按下和释放动作...
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
现在你可以复制粘贴三次,但我只是把它放在一个循环中
您可以在字符串中输入值,然后您可以按照 Eng.Fouad 的说明使用该字符串。不过用起来没什么乐趣,你可以试试这个
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
你也可以使用Thread.sleep这样它可以慢慢输入数据位。
我想我已经实现了更好的解决方案,也许有人发现它有用(主要方法是从枚举 KeyCode 中读取所有值,而不是将其放入 HashMap 并稍后使用它来查找 int 键代码)
public class KeysMapper {
private static HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
static {
for (KeyCode keyCode : KeyCode.values()) {
if (keyCode.impl_getCode() >= 65 && keyCode.impl_getCode() <= 90){
charMap.put(keyCode.getName().toLowerCase().toCharArray()[0], keyCode.impl_getCode());
}
else{
charMap.put(keyCode.getName().toLowerCase().toCharArray()[0], keyCode.impl_getCode());
}
}
}
public static Key charToKey(char c){
if(c>=65 && c<=90){
return new Key(charMap.get(c), true);
} else {
return new Key(charMap.get(c), false);
}
}
public static List<Key> stringToKeys(String text){
List<Key> keys = new ArrayList<Key>();
for (char c : text.toCharArray()) {
keys.add(charToKey(c));
}
return keys;
}
我还创建了一个键 class 来知道是输入大写字符还是小写字符:
public class Key {
int keyCode;
boolean uppercase;
//getters setter constructors}
最后你可以像那样使用它(对于单个字符)robot.keyPress(charToKey('a').getKeyCode());
如果你想按大写字母,你必须同时按下和释放 shift 键
从 Java 7 开始,您还可以使用 KeyEvent.getExtendedKeyCodeForChar(c)。仅小写字母的示例可以是:
void sendKeys(Robot robot, String keys) {
for (char c : keys.toCharArray()) {
int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
if (KeyEvent.CHAR_UNDEFINED == keyCode) {
throw new RuntimeException(
"Key code not found for character '" + c + "'");
}
robot.keyPress(keyCode);
robot.delay(100);
robot.keyRelease(keyCode);
robot.delay(100);
}
}
这不会键入整个 "string" 但有助于一次键入一个字符以外的任何内容。
Runtime.getRuntime().exec("notepad.exe");//or anywhere you want.
Thread.sleep(5000);//not required though gives a good feel.
Robot r=new Robot();
String a="Hi My name is Whatever you want to say.";
char c;
int d=a.length(),e=0,f=0;
while(e<=d)
{
c=a.charAt(e);
f=(int) c; //converts character to Unicode.
r.keyPress(KeyEvent.getExtendedKeyCodeForChar(f));
e++;
Thread.sleep(150);
}
看到它完美运行,太棒了!
虽然它不适用于 unicode 无法追踪的特殊字符,如 |,!...等。
StringSelection path = new StringSelection("path of your document ");
// create an object to desktop
Toolkit tol = Toolkit.getDefaultToolkit();
// get control of mouse cursor
Clipboard c = tol.getSystemClipboard();
// copy the path into mouse
c.setContents(path, null);
// create a object of robot class
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
我已经知道如何使用 java.awt.Robot
使用 keyPress
键入单个字符,如下所示。我如何才能将 whole 预定义的 String
值 一次 简单地输入文本框?
robot.keyPress(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_1);
// instead, enter String x = "111"
常见的解决方案是使用剪贴板:
String text = "Hello World";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
您需要 "type" 角色,这是一个按下和释放动作...
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
现在你可以复制粘贴三次,但我只是把它放在一个循环中
您可以在字符串中输入值,然后您可以按照 Eng.Fouad 的说明使用该字符串。不过用起来没什么乐趣,你可以试试这个
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
你也可以使用Thread.sleep这样它可以慢慢输入数据位。
我想我已经实现了更好的解决方案,也许有人发现它有用(主要方法是从枚举 KeyCode 中读取所有值,而不是将其放入 HashMap 并稍后使用它来查找 int 键代码)
public class KeysMapper {
private static HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
static {
for (KeyCode keyCode : KeyCode.values()) {
if (keyCode.impl_getCode() >= 65 && keyCode.impl_getCode() <= 90){
charMap.put(keyCode.getName().toLowerCase().toCharArray()[0], keyCode.impl_getCode());
}
else{
charMap.put(keyCode.getName().toLowerCase().toCharArray()[0], keyCode.impl_getCode());
}
}
}
public static Key charToKey(char c){
if(c>=65 && c<=90){
return new Key(charMap.get(c), true);
} else {
return new Key(charMap.get(c), false);
}
}
public static List<Key> stringToKeys(String text){
List<Key> keys = new ArrayList<Key>();
for (char c : text.toCharArray()) {
keys.add(charToKey(c));
}
return keys;
}
我还创建了一个键 class 来知道是输入大写字符还是小写字符:
public class Key {
int keyCode;
boolean uppercase;
//getters setter constructors}
最后你可以像那样使用它(对于单个字符)robot.keyPress(charToKey('a').getKeyCode());
如果你想按大写字母,你必须同时按下和释放 shift 键
从 Java 7 开始,您还可以使用 KeyEvent.getExtendedKeyCodeForChar(c)。仅小写字母的示例可以是:
void sendKeys(Robot robot, String keys) {
for (char c : keys.toCharArray()) {
int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
if (KeyEvent.CHAR_UNDEFINED == keyCode) {
throw new RuntimeException(
"Key code not found for character '" + c + "'");
}
robot.keyPress(keyCode);
robot.delay(100);
robot.keyRelease(keyCode);
robot.delay(100);
}
}
这不会键入整个 "string" 但有助于一次键入一个字符以外的任何内容。
Runtime.getRuntime().exec("notepad.exe");//or anywhere you want.
Thread.sleep(5000);//not required though gives a good feel.
Robot r=new Robot();
String a="Hi My name is Whatever you want to say.";
char c;
int d=a.length(),e=0,f=0;
while(e<=d)
{
c=a.charAt(e);
f=(int) c; //converts character to Unicode.
r.keyPress(KeyEvent.getExtendedKeyCodeForChar(f));
e++;
Thread.sleep(150);
}
看到它完美运行,太棒了! 虽然它不适用于 unicode 无法追踪的特殊字符,如 |,!...等。
StringSelection path = new StringSelection("path of your document ");
// create an object to desktop
Toolkit tol = Toolkit.getDefaultToolkit();
// get control of mouse cursor
Clipboard c = tol.getSystemClipboard();
// copy the path into mouse
c.setContents(path, null);
// create a object of robot class
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}