天天看點

Java Rebot類

java的Robot類的幾個簡單例子

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.event.KeyEvent;

public class TestRobot {

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

TestRobot t=new TestRobot();

t.test();

t.test1();

}

public void test()throws AWTException

{

   Robot robot=new Robot();

    robot.keyPress(KeyEvent.VK_CONTROL);

    robot.keyPress(KeyEvent.VK_ALT);

    robot.keyPress(KeyEvent.VK_DELETE);

    robot.keyRelease(KeyEvent.VK_DELETE);

    robot.keyRelease(KeyEvent.VK_CONTROL);

    robot.keyRelease(KeyEvent.VK_ALT);

}

public void test1()throws AWTException{

   Robot robot=new Robot();

robot.keyPress(KeyEvent.VK_CONTROL);

     robot.keyPress(KeyEvent.VK_SHIFT);

     robot.keyPress(KeyEvent.VK_ESCAPE);

     robot.keyRelease(KeyEvent.VK_ESCAPE);

     robot.keyRelease(KeyEvent.VK_SHIFT);

     robot.keyRelease(KeyEvent.VK_CONTROL);

}

}

第二個例子:

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.event.KeyEvent;

import java.io.IOException;

public class RobotExp {

public static void pressKey(Robot robot, int keyvalue) {

robot.keyPress(keyvalue);

robot.keyRelease(keyvalue);

}

public static void pressKeyWithShift(Robot robot, int keyvalue) {

robot.keyPress(KeyEvent.VK_SHIFT);

robot.keyPress(keyvalue);

robot.keyRelease(keyvalue);

robot.keyRelease(KeyEvent.VK_SHIFT);

}

public static void closeApplication(Robot robot) {

// pressKey(robot, KeyEvent.VK_ALT);

// pressKey(robot, KeyEvent.VK_F4);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_F4);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyRelease(KeyEvent.VK_F4);

//for linux.

// robot.keyPress(KeyEvent.VK_ALT);

// robot.keyPress(KeyEvent.VK_W);

// robot.keyRelease(KeyEvent.VK_ALT);

// robot.keyRelease(KeyEvent.VK_W);

robot.keyPress(KeyEvent.VK_N);

robot.keyRelease(KeyEvent.VK_N);

}

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

try {

Robot robot = new Robot();

Runtime.getRuntime().exec("notepad");

// For linux.

//Runtime.getRuntime().exec("gedit");

//定義5秒的延遲以便你打開notepad 哈哈

// Robot 開始寫

robot.delay(3000);

for (int i = 0; i < 100; i++) {

pressKeyWithShift(robot, KeyEvent.VK_H);

pressKey(robot, KeyEvent.VK_I);

pressKey(robot, KeyEvent.VK_SPACE);

//pressKeyWithShift(robot, KeyEvent.VK_H);

pressKeyWithShift(robot, KeyEvent.VK_I);

pressKey(robot, KeyEvent.VK_SPACE);

pressKey(robot, KeyEvent.VK_A);

pressKey(robot, KeyEvent.VK_M);

pressKey(robot, KeyEvent.VK_SPACE);

pressKey(robot, KeyEvent.VK_T);

pressKey(robot, KeyEvent.VK_H);

pressKey(robot, KeyEvent.VK_E);

pressKey(robot, KeyEvent.VK_SPACE);

pressKey(robot, KeyEvent.VK_J);

pressKey(robot, KeyEvent.VK_A);

pressKey(robot, KeyEvent.VK_V);

pressKey(robot, KeyEvent.VK_A);

pressKey(robot, KeyEvent.VK_SPACE);

pressKey(robot, KeyEvent.VK_R);

pressKey(robot, KeyEvent.VK_O);

pressKey(robot, KeyEvent.VK_B);

pressKey(robot, KeyEvent.VK_O);

pressKey(robot, KeyEvent.VK_T);

// VK_ENTER

pressKey(robot, KeyEvent.VK_ENTER);

//pressKey(robot, KeyEvent.);

}

closeApplication(robot);

//robot.keyPress(KeyEvent.VK_SPACE);

} catch (AWTException e) {

e.printStackTrace();

}

}

}

第一個例子是ctrl+shift+tab鍵打開任務管理器

第二個例子是在記事本輸入"Hi I am the java robot" ,然後alt+F4關閉記事本

附上Java Rebot中JDK的解釋:

public class Robot extends Object

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example,

Robot.mouseMove

will actually move the mouse cursor instead of just generating mouse move events.

Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an

AWTException

will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server.

Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully.

Constructor Summary

Robot()

          Constructs a Robot object in the coordinate system of the primary screen.

Robot(GraphicsDevice screen)

          Creates a Robot for the given screen device.
Method Summary

 BufferedImage

createScreenCapture(Rectangle screenRect)

          Creates an image containing pixels read from the screen.

 void

delay(int ms)

          Sleeps for the specified time.

 int

getAutoDelay()

          Returns the number of milliseconds this Robot sleeps after generating an event.

 Color

getPixelColor(int x, int y)

          Returns the color of a pixel at the given screen coordinates.

 boolean

isAutoWaitForIdle()

          Returns whether this Robot automatically invokes

waitForIdle

after generating an event.

 void

keyPress(int keycode)

          Presses a given key.

 void

keyRelease(int keycode)

          Releases a given key.

 void

mouseMove(int x, int y)

          Moves mouse pointer to given screen coordinates.

 void

mousePress(int buttons)

          Presses one or more mouse buttons.

 void

mouseRelease(int buttons)

          Releases one or more mouse buttons.

 void

mouseWheel(int wheelAmt)

          Rotates the scroll wheel on wheel-equipped mice.

 void

setAutoDelay(int ms)

          Sets the number of milliseconds this Robot sleeps after generating an event.

 void

setAutoWaitForIdle(boolean isOn)

          Sets whether this Robot automatically invokes

waitForIdle

after generating an event.

 String

toString()

          Returns a string representation of this Robot.

 void

waitForIdle()

          Waits until all events currently on the event queue have been processed.