This is for all you java programmers.Sometimes there could be situations where you might need to do some repititive tasks such as getting some data from a website or entering some inputs which includes opening some applications and entering some keystrokes or mouse clicks.
Most of the software we develop is to make things easier ,to make things faster in short to automate.Incase you can't do this by pure scripting.Then Java Robot can be your saviour.Just enter codes as to where the mouse should move and where it should click and where you have to type what and thats it..You have your Robot -JAVA ROBOT.
Let me give you a few important functions in java Robot
Robot robot = new Robot(); //to initialize a robot
robot.mouseMove(245, 10); //to move the mouse to a particular place
robot.mousePress( InputEvent.BUTTON1_MASK ); //press left mouse button
robot.mouseRelease( InputEvent.BUTTON1_MASK );
robot.delay(4000); //wait for 4 seconds
robot.keyPress(KeyEvent.VK_W); //press letter W
robot.keyRelease(KeyEvent.VK_W);
The mouseMove() can be used to move the mouse anywhere in the screen, to get the exact location can be tough..Its pure trial and error method,but you will surely get the right x and y axis in the end.
Down below is a sample program to just wait for sometime and move the mouse cursor to a specific location..This is just to show you the imports and stuff.You can get more creative and make your own cool automation programs.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.awt.event.InputEvent;
public class pos {
public static void main(String[] args)throws AWTException, IOException{
Robot robot = new Robot();
robot.delay(4000);
robot.mouseMove(63,472);
robot.delay(1000);
}
}
|
0
comments
]
0 comments
Post a Comment