Pregunta

I am developing a program which simulates key presses in Java, in a human-like way. The objective is to send random key presses every x seconds (x is a random number between two integers). Here is the code I have so far:

public class AutoKeyboard {

    public static int randInt(int min, int max) { // Method to generate random int
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }

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

        int running = 1;

        while (running == 1) {
            try {

                int delay = randInt(336415,783410); // Generates random int between two integers
                Robot robot = new Robot();

                Thread.sleep(delay); // Thread sleeps for x (random int) milliseconds
                robot.keyPress(KeyEvent.VK_SPACE); // Simulating press of space bar

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

What I would like to achieve is the Keyevent.VK_SPACE to be random, so instead of the Spacebar it could be any key from a list (e.g. it would press a random key from A-D). How would I go about doing this? I can't think of a logical solution with the programming knowledge I already have (which is minimal sadly)

Thank you for any of your responses.

¿Fue útil?

Solución 2

I would just generate a random number between 33 & 126. Then every couple of char's randomly add a 32. 32 is a space. 65 - 90 are upper case, and 97-122 are lower case.

Good resource: http://www.asciitable.com/

To generate a random number I would:

Random randomGenerator = new Random();
int ran = randomGenerator.nextInt(126 - 33);
ran += 33;

Then ran would be your random character. You could have a loop and do that a couple times.

You don't even need to use KeyEvent.VK_SPACE. If you use:

System.out.println(KeyEvent.VK_SPACE);

You will get a return of "32". That corresponds to the ASCII chart, just use that.

Otros consejos

You can define an array (or a List if you prefer) somewere (final static would be nice), containing all the possible keys you want to be able to press. This could be rather verbose, but gives you the flexibility to use any key you want:

int possibleKeys = new int[]{
    KeyEvent.VK_SPACE, 
    KeyEvent.VK_0,
    KeyEvent.VK_A,
    KeyEvent.VK_UP
};

You can even use the corresponding chars, for the constants that share the same ASCII value:

int possibleKeys = new int[]{
    ' ',
    '0',
    'A', // careful not to use 'a' though!
    KeyEvent.VK_UP
};

Then just pick one random key code from that array:

Random rand = new Random();
int i = rand.nextInt(possibleKeys.length);
int keyCode = possibleKeys[i];

Also, I see you are missing a robot.keyRelease that should be done after the robot.keyPress:

robot.keyPress(keyCode);   // Simulating press of a key
robot.keyRelease(keyCode); // Simulating release of a key

Since KeyEvent defines plain integers for the keys, you can use them instead of KeyEvent.VK_X. It shouldn't be a problem to get some random integers then.
Please visit http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html for keycodes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top