我在网上找到了Java代码,让我可以使用Robot类做各种事情。我目前正在使用它来尝试自动化需要重复输入文件名的数据库转储,在本例中为数字。不,我不能只在最后重命名批处理文件,它们必须独立命名。我不想这样做13,000次(我的数据库的大小)所以我试图自动化它。我需要的是没有机器人代码来输入For循环中的内容。我的代码如下:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class RobotClicker
{
Robot robot = new Robot();
public static void main(String[] args) throws AWTException
{
new RobotClicker();
}
public RobotClicker() throws AWTException
{
robot.setAutoDelay(40);
robot.setAutoWaitForIdle(true);
robot.delay(4000);
robot.mouseMove(144, 400);
robot.delay(500);
leftClick();
robot.delay(500);
leftClick();
robot.delay(500);
for(int i = 856; i>858;i++ ){
String holding = "test ";
type(holding);
}
robot.mouseMove(450, 480);
robot.delay(500);
leftClick();
robot.delay(500);
leftClick();
robot.delay(1000);
System.exit(0);
}
private void leftClick()
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}
private void type(int i)
{
robot.delay(40);
robot.keyPress(i);
robot.keyRelease(i);
}
private void type(String s)
{
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;
robot.delay(40);
robot.keyPress(code);
robot.keyRelease(code);
}
}
}
文件名只是数字。我需要for循环来生成序列中的下一个数字,然后机器人输入它,重复广告恶心。目前机器人正在移动并且点击很好,但没有任何类型。任何帮助将不胜感激。
答案 0 :(得分:1)
这是一个可以输入字母和数字的简单示例:
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");
typeCharacter(robot, "%");
}
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);
}
}
}
答案 1 :(得分:0)
这是我最终得到的代码。我启动数据库转储(打开“保存”对话框。我将对话框移动到屏幕的左上角。屏幕“X”和“Y”位置基于我的屏幕,我移动了对话框到。),然后启动机器人程序。它将鼠标移动到对话框的“文件名”部分(根据我将框移动到的位置),单击鼠标左键一次,输入序列中的下一个数字,然后将鼠标移动到左键单击“保存”按钮是。等待一段时间,然后重复。它在一夜之间完成了2个数据库的全部工作,所以我们不再需要它了。耶科技!
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.awt.*;
import java.util.*;
import java.lang.reflect.Field;
import java.awt.event.*;
import javax.swing.*;
public class RobotClicker {
Robot robot = new Robot();
public static void main(String[] args) throws AWTException {
new RobotClicker();
}
public RobotClicker() throws AWTException {
robot.setAutoDelay(40);
robot.setAutoWaitForIdle(true);
for (int i = 0; i < 7476; i++) {
String holding = Integer.toString(i);
Robot robot = new Robot();
robot.delay(4000);
robot.mouseMove(144, 400);
robot.delay(500);
leftClick();
//robot.delay(500);
leftClick();
robot.delay(10);
type(holding);
robot.mouseMove(450, 480);
robot.delay(10);
leftClick();
robot.delay(500);
leftClick();
robot.delay(1000);
}
System.exit(0);
}
private void leftClick() {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}
private void type(int i) {
robot.delay(40);
robot.keyPress(i);
robot.keyRelease(i);
}
private void type(String s) {
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;
robot.delay(40);
robot.keyPress(code);
robot.keyRelease(code);
}
}
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);
}
}
}