4

I have a problem with Robot class in Java: it sometimes doesn't work when i run a fullscreen game. Here's my code:

Toolkit tlkt = Toolkit.getDefaultToolkit();
Robot bot = new Robot();
while(true) {
    Thread.sleep(3000);
    tlkt.beep(); //make sure that program still runs
    bot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
    bot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
}

I want to left-click every 3 seconds and beep for sure. It work fine but when I run some full-screen application, then it just beeps but not click.

I already tried to move the Robot declaration into the loop, so new Robot is created every time, but with no sucess.

I have tried it on 5 different games, on 3 of them it worked fine even with fullscreen, but on 2 it didnt work but only beeped. Any help?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kajacx
  • 12,361
  • 5
  • 43
  • 70

1 Answers1

4

Assuming you are on a Windows machine, the call to java.awt.Robot calls are redirected to sun.awt.windows.WRobotPeer, which in turn calls native code in awt_Robot.cpp. You can check the source code of the awt_Robot.cpp here : http://www.koders.com/cpp/fidFFE004659A9CAB3DA2B3302C457E624AF6F3EEDF.aspx?s=GetDIBits#L232

Here you can see that the mouse events are realized with win32 call mouse_event(...) (defined in winuser.h)

So your Robot mouse click calls are limited by the limitations of mouse_event(...).

Then a little googling on this, reveals that some games have some sort of macro protection mechanism. They ignore mouse_event(...) routed methods and talk directly with the driver. Hackshield, for instance, provides such protection mechanisms to a number of games. So this is the reason why some games do not receive your Robot mouse clicks.

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
  • OK, i guess it would be really painful to install a mouse driver which could comunicate with my java program? – kajacx May 24 '12 at 14:48
  • As far as I understand, Hackshield , for instance, installs and uses its own drivers. – Hakan Serce May 24 '12 at 14:52
  • Fine, lets try this: lets say i want to have imaginary mouse controlled by java program, but it would behave like absolutely normal mouse, like touchpad or actual mouse. If i clicked "Control panel -> devices -> mouses" i would see it here among other mouse devices. Is this possible? – kajacx May 24 '12 at 15:12
  • I am not sure, you need to check that. In the mean time, can you check the game installation folders for a folder named HShield. If you can find that, then we are on track. – Hakan Serce May 24 '12 at 17:16