简体   繁体   中英

robot.keyPress won't work properly in java for game trainer

I want to create a game trainer for mount and blade, the game itself have cheat features in it and I want to take advantage of it.

For example when you want increase your money you should press ctrl + x but it's boring.

So I create a code that if you press ctrl + x 100 times for me but it's just working once and not 100 times.

try {
    Robot robot = new Robot();
    robot.delay(5000);
    robot.keyPress(KeyEvent.VK_CONTROL);

    for(int y = 0; y < 100; y++)
    {
        robot.keyPress(KeyEvent.VK_X);
    }
} catch (AWTException e) {
    e.printStackTrace();
}

Note:

  1. If I put

     robot.keyPress(KeyEvent.VK_CONTROL) 

    in the for loop, the code won't work

  2. If I put any robot.keyRelease in code it stops working

Why is it just working one time?

From the documentation , if you press a key, it will be pressed until you released it. So if you press Ctrl + X and then you press X in the loop you will be getting this Ctrl + X + X ...

try {
    Robot robot = new Robot();           
    robot.delay(5000);
    for(int y=0;y < 100 ; y++)
    {
      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.keyPress(KeyEvent.VK_X);   

      robot.keyRelease(KeyEvent.VK_CONTROL);   
      robot.keyRelease(KeyEvent.VK_X);  
    }
    } catch (AWTException e) {
       e.printStackTrace();
    } 

EDIT:

Also, as XtremeBaumer said in the comments, probably the game could detect you are cheating if you "press" the keys so fast. Adding some delay between each iteration would make it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM