我使用了以下循环,但它只输入第一个字符,其余为数字,任何想法?
import java.awt.*;
import javax.swing.KeyStroke;
public class test {
public static void main(String[] args) throws AWTException
{
Robot r = new Robot();
String s = "Face";
for (int i = 0; i < s.length(); i++)
{
char res = s.charAt(i);
r.keyPress(res);
r.keyRelease(res);
r.delay(1000);
}
}
}
输出输入:F135
答案 0 :(得分:10)
keyPress / Release方法需要一个int值,表示要键入的字符。这些值是由KeyEvent.VK _ ???确定的每个字符的关键代码。变量
尝试:
import java.awt.*;
import java.util.*;
import java.lang.reflect.Field;
import java.awt.event.*;
import javax.swing.*;
public class RobotCharacter
{
public static void main(String[] args)
throws Exception
{
JTextField textField = new JTextField(10);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( textField );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
Robot robot = new Robot();
typeCharacter(robot, "a");
typeCharacter(robot, "b");
typeCharacter(robot, "C");
typeCharacter(robot, "D");
}
public static void typeCharacter(Robot robot, String letter)
{
try
{
boolean upperCase = Character.isUpperCase( letter.charAt(0) );
String variableName = "VK_" + letter.toUpperCase();
Class clazz = KeyEvent.class;
Field field = clazz.getField( variableName );
int keyCode = field.getInt(null);
robot.delay(1000);
if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );
robot.keyPress( keyCode );
robot.keyRelease( keyCode );
if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
}
catch(Exception e)
{
System.out.println(e);
}
}
}
然而,即使这对所有角色都不起作用。例如,在我的键盘上,&#34;%&#34;高于&#34; 5&#34;。您无法使用VK_PERCENT。所需的关键行程是VK_5以及换档。无法知道键盘的实际映射以自动执行此操作。
所以机器人不是一个很好的方法。
答案 1 :(得分:1)
Robot类使用此处定义的键代码:http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/KeyEvent.html,而不是原始字符。你需要这样称呼它:
r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);
答案 2 :(得分:0)
@ camickr我对你的scipt做了一个小编辑,以容纳一个字符串而不是一个字母。如下请见 。它帮助我谢谢:)函数调用:typeCharacter(robot, "thanks");
public static void typeCharacter(Robot robot, String letter)
{
for(int i=0;i<letter.length();i++){
try
{
boolean upperCase = Character.isUpperCase( letter.charAt(i) );
String KeyVal=Character.toString(letter.charAt(i));
String variableName = "VK_" + KeyVal.toUpperCase();
Class clazz = KeyEvent.class;
Field field = clazz.getField( variableName );
int keyCode = field.getInt(null);
robot.delay(1000);
if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );
robot.keyPress( keyCode );
robot.keyRelease( keyCode );
if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
}
catch(Exception e)
{
System.out.println(e);
}
}
}
答案 3 :(得分:0)
您还可以将String加载到剪贴板,然后将其粘贴到您想要的位置。
StringSelection selection = new StringSelection("Hello World");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
robo.keyPress(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_CONTROL);
答案 4 :(得分:0)
如果您创建一个接收要键入的字符串的函数,则可以让您的生活变得轻松:
public void keyboardString(final String text) {
if (text != null) {
try {
final Robot robot = new Robot();
for (int i = 0; i < text.length(); i++) {
final char ch = text.charAt(i);
final boolean upperCase = Character.isUpperCase(ch);
final int keyCode = KeyEvent.getExtendedKeyCodeForChar(ch);
robot.delay(10);
if (upperCase) {
robot.keyPress(KeyEvent.VK_SHIFT);
}
robot.keyPress(keyCode);
robot.keyRelease(keyCode);
if (upperCase) {
robot.keyRelease(KeyEvent.VK_SHIFT);
}
}
} catch (final Exception e) {
System.out.println(e);
}
}
}