0

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]); 
2
Contributors
8
Replies
12
Views
4 Years
Discussion Span
Last Post by godzab
Featured Replies
  • 1

    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, ...); Read More

0

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?

0

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.

0

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
0

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

0

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.

1

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, ...);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.