Question

I'm having a little bit of a play around with the Java Robot function.

However as i think it's a major pain in the backside i was wondering if i could abstract the function and create a faster way.

Now Firstly i have done the following:

public abstract class Cyborg {

    public static void Cyborg(KeyEvent args[]) throws AWTException{
        try {
            Robot robot = new Robot(); 
            for(KeyEvent k:args){
                robot.keyPress(KeyEvent.k); 
            }
        } catch(AWTException e){
            e.printStackTrace();
        }
    }
}

Which i am currently a little baffled with i am currently getting an error that k cannot be resolved. However in the KeyEvent args[] section should this be classed as a KeyEvent or should this just be a string? As i am from a PHP background i am starting to get to grips with java. Can anyone advise if this is the best way to go about this? And also as to why KeyEvent.k won't resolve whether it is a KeyEvent or if it is a String? Any other advise on if this will work/won't work or issues to try and avoid would be great.

Thank you

Was it helpful?

Solution

robot.keyPress(KeyEvent.k); 

This won't work, because this will attempt to find a static member of the class KeyEvent called k. You've named your incremental object k, so use k.

robot.keyPress(k.getKeyCode());

More thorough Explanation

When you say the following:

 String[] strs = {"hello", "there", "my", "friend"};

 for(String str : strs)
 {
     // Do something.
 }

What you're making the computer do has the same effect as:

 for(int x = 0; x < strs.length; x++)
 {
     String str = strs[x];
 }

So you're defining a String object named str that will contain the current String object that is contained within the strs array.

OTHER TIPS

This line

robot.keyPress(KeyEvent.k); 

Should be like this

robot.keyPress(k.getKeyCode());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top