Hello guys, I need help with the Robot class in Java. I am trying to excute a couple of keystrokes using an array so it will be less repetitive typing in the keyPress method. Here is the code:

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 
import java.io.IOException;

public class RobotExp { 

    public static void main(String[] args)throws IOException { 

            try { 
            char a[] = {'H', 'E', 'L', 'L', 'O'};

            Robot robot = new Robot(); 

            robot.delay(5000); 

            for(int i = 0; i < a.length; i++)
            {
            robot.keyPress(KeyEvent.VK_a[i]); 

            }
            robot.mouseMove(100,-10); 


            } catch (AWTException e) { 
        e.printStackTrace(); 
        } 
    } 
}



the problem is I get his error when compiling the code:
RobotExp.java:19: error: cannot find symbol
    robot.keyPress(KeyEvent.VK_a[i]); 

What symbol was not found? Where was the ^ posititioned?

What does the API doc say for the keyPress() method? What type of argument does it take?
Did you give it the correct argument?

Sorry the ^ is posititioned at the period, right after the KeyEvent.Here is what it can take:
public void keyPress(int keycode). So how could I use an array to do multiple commands.

You forgot to say what was the symbol that was not found?

Sorry I did not give the full Error:

RobotExp.java:19: error: cannot find symbol
    robot.keyPress(KeyEvent.VK_a[i]); 
                           ^
  symbol:   variable VK_a
  location: class KeyEvent
1 error

The compiler can not find the field: VK_a in the KeyEvent class. Why is that line coded with that variable?

Because I would like to use an array to do the KeyEvent. It's to repetitive doing this:
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);

I want to know if you could use an array for that.

You can't make up members of the KeyEvent class. Either use the variables from that class or your own variables.
Look at the definitions for the KeyEvent members: VK_. You can create an array of them:
int[] vkcars = {KeyEvent.VK_H, ...);

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.