Search code examples
javafor-loopawtrobot

Get Java Robot to type what is in a for loop


I have found Java code online that will let me use the Robot class to do various things. I am using it currently to try to automate a database dump that requires repetitive entering of file names, in this case numbers. No, I cannot just do a batch file rename at the end, they have to be named independently. I don't want to do this 13,000 times (the size of my database) so I am attempting to automate it. What I need that I don't have is the Robot code to type what is in the For loop. My code is below:

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

The file names are just numbers. I need the for loop to produce the next number in the sequence, and then the Robot to type it out, repeat ad nauseam. At the moment the Robot does the moving and the clicking just fine, but nothing types out. Any help would be greatly appreciated.


Solution

  • Here is the code that I ended up with. I start the database dump (which brings up the "save" dialog box. I moved the dialog box to the upper left corner of the screen. The screen "X" and "Y" positions are based on my screen and where I moved the dialog box to.), then start the robot program. It moves the mouse to the "File name" part of the dialog box (based on where I moved the box to), clicks the left mouse button once, enters the next number in the sequence, then moves the mouse to left click where the "save" button is. It waits for a brief period then repeats. It did the entirety of the 2 databases overnight so we didn't have to mess with it anymore. Yay technology!

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