获取 Java 机器人来键入 for 循环中的内容
Get Java Robot to type what is in a for loop
我在网上找到 Java 代码,可以让我使用机器人 class 做各种事情。我目前正在使用它来尝试自动执行需要重复输入文件名(在本例中为数字)的数据库转储。不,我不能只在最后做一个批处理文件重命名,它们必须独立命名。我不想这样做 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 循环来生成序列中的下一个数字,然后机器人将其键入,重复令人作呕的过程。目前,机器人可以正常移动和点击,但没有输入任何内容。任何帮助将不胜感激。
这是一个可以输入字母和数字的简单示例:
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);
}
}
}
这是我最终得到的代码。我启动数据库转储(这会弹出 "save" 对话框。我将对话框移到了屏幕的左上角。屏幕 "X" 和 "Y" 位置是基于我的屏幕以及我将对话框移动到的位置。),然后启动机器人程序。它将鼠标移动到对话框的 "File name" 部分(基于我将框移动到的位置),单击鼠标左键一次,输入序列中的下一个数字,然后将鼠标移动到左键单击的位置"save" 按钮是。它会等待一小段时间然后重复。它在一夜之间完成了 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);
}
}
}
我在网上找到 Java 代码,可以让我使用机器人 class 做各种事情。我目前正在使用它来尝试自动执行需要重复输入文件名(在本例中为数字)的数据库转储。不,我不能只在最后做一个批处理文件重命名,它们必须独立命名。我不想这样做 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 循环来生成序列中的下一个数字,然后机器人将其键入,重复令人作呕的过程。目前,机器人可以正常移动和点击,但没有输入任何内容。任何帮助将不胜感激。
这是一个可以输入字母和数字的简单示例:
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);
}
}
}
这是我最终得到的代码。我启动数据库转储(这会弹出 "save" 对话框。我将对话框移到了屏幕的左上角。屏幕 "X" 和 "Y" 位置是基于我的屏幕以及我将对话框移动到的位置。),然后启动机器人程序。它将鼠标移动到对话框的 "File name" 部分(基于我将框移动到的位置),单击鼠标左键一次,输入序列中的下一个数字,然后将鼠标移动到左键单击的位置"save" 按钮是。它会等待一小段时间然后重复。它在一夜之间完成了 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);
}
}
}