Reputation: 4378
Hi guys
I want to win an hard videogame on my pc. The rule is simple: when the screen turns all white, i have to press immediately the spacebar. I have to do it 200 times :/
Maybe a simple Java application would help me :P something like this:
new Thread(){
public void run(){
while(true){
Color c = getPredominantColorOfTheScreen();
if (c.equals(Color.white)){
pressTheSpacebar();
}
sleep(10);
}
}
}.start();
Can Someone help me to write these two functions? :)
The game is fullscreen
Upvotes: 2
Views: 395
Reputation: 72389
The class you're after is the robot class. In terms of the key press, keyPress(KeyEvent.VK_SPACE)
will do it. In terms of the screen colour, robot will get the colour of an individual pixel rather than the predominant colour of the screen (use the getPixelColor()
method. However, if the whole screen turns this colour (or just a regular part of the screen) you only actually need to check 1 pixel.
Upvotes: 2
Reputation: 3242
You will need to look into the Java.awt.Robot class to do both of these things.
The createScreenCapture()
function will take a screenshot of the screen.
You will then need to use the keyPress()
function to press the spacebar.
In addition to that you're likely to need to switch the application that is in focus when pressing the spacebar, and you could probably do this by using either an alt-tab
key press combination, or moving the mouse pointer over the game window and clicking there. Or if neither of those methods work, move the mouse pointer over the taskbar/dock/whatever your platform uses and trigger a click there.
Look at this webpage for more help with this.
http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Robot.html
Upvotes: 1
Reputation: 346526
Take a look at the class java.awt.Robot
- it contains methods that you should be able to use for this purpose. I suggest that instead of computing the "predominant color of the screen", which could be rather slow, you just look at a capture of a small part somewhere in the middle and whether it's all white or not.
Upvotes: 1
Reputation: 29540
I know no way to access the video memory with java without using native functions.
Maybe you could have a look at Jogl but I fear even this won't allow you to access the video memory of another application.
Injecting keys into the keyboard buffer will need to use native functions like SendInput
or keybd_event
.
Upvotes: 0