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
 BufferedImagecreateScreenCapture(Rectangle screenRect)
          Creates an image containing pixels read from the screen.
 voiddelay(int ms)
          Sleeps for the specified time.
 intgetAutoDelay()
          Returns the number of milliseconds this Robot sleeps after generating an event.
 ColorgetPixelColor(int x, int y)
          Returns the color of a pixel at the given screen coordinates.
 booleanisAutoWaitForIdle()
          Returns whether this Robot automatically invokes waitForIdle after generating an event.
 voidkeyPress(int keycode)
          Presses a given key.
 voidkeyRelease(int keycode)
          Releases a given key.
 voidmouseMove(int x, int y)
          Moves mouse pointer to given screen coordinates.
 voidmousePress(int buttons)
          Presses one or more mouse buttons.
 voidmouseRelease(int buttons)
          Releases one or more mouse buttons.
 voidmouseWheel(int wheelAmt)
          Rotates the scroll wheel on wheel-equipped mice.
 voidsetAutoDelay(int ms)
          Sets the number of milliseconds this Robot sleeps after generating an event.
 voidsetAutoWaitForIdle(boolean isOn)
          Sets whether this Robot automatically invokes waitForIdle after generating an event.
 StringtoString()
          Returns a string representation of this Robot.
 voidwaitForIdle()
          Waits until all events currently on the event queue have been processed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值