Robot类实例

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;

/**
 *
 */

/**
 * @author samuel
 *
 */
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();
  }
 }

}

 

 

 

随机移动鼠标并点击左键。

代码如下:

import java.awt.AWTException;

import java.awt.Dimension;

import java.awt.Robot;

import java.awt.Toolkit;

import java.awt.event.InputEvent;

import java.util.Random;


/**

*

* @author Xiaofeng Wang

*/

public class MouseController implements Runnable {

private Dimension dim;

private Random rand;

private Robot robot;

private volatile boolean stop = false;

/** Creates a new instance of Main */

public MouseController() {

dim = Toolkit.getDefaultToolkit().getScreenSize();

rand = new Random();

try {

robot = new Robot();

} catch (AWTException ex) {

ex.printStackTrace();

}

}


public void run() {

while(!stop) {

int x = rand.nextInt(dim.width);

int y = rand.nextInt(dim.height);

robot.mouseMove(x, y);

robot.mousePress(InputEvent.BUTTON1_MASK);

try {

Thread.sleep(3000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

public synchronized void stop() {

stop = true;

}

/** * @param args the command line arguments */

public static void main(String[] args) {

MouseController mc = new MouseController();

Thread mcThread = new Thread(mc);

System.out.println("Mouse Controller start");

mcThread.start();

try {

Thread.sleep(60000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

mc.stop();

System.out.println("Mouse Controller stoped");

}

}

 

 

 

使用robot截屏,实现Capture程序,每隔1秒截屏一次。

代码如下:

public class Capture extends javax.swing.JFrame implements Runnable {

/** Creates new form Capture */

public Capture() {

initComponents();

try {

robot = new Robot();

} catch (AWTException ex) {

ex.printStackTrace();

}

dim = Toolkit.getDefaultToolkit().getScreenSize(); }

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold desc=" 生成的代码 " defaultstate="collapsed"></editor-fold>

private void initComponents() {

screenCanvas = new java.awt.Canvas();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

stop = true;

setResizable(false);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 519, javax.swing.GroupLayout.PREFERRED_SIZE) );

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)

);

pack();

}//

/** * @param args the command line arguments */

public static void main(String args[]) {

final Capture capture = new Capture();

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

capture.setVisible(true);

}

});

Thread cutThread = new Thread(capture);

cutThread.start();

}

public void run() {

stop = false;

while(!stop) {

BufferedImage bImage = robot.createScreenCapture(new Rectangle(dim.width, dim.height));

Graphics g = this.screenCanvas.getGraphics();

g.drawImage(bImage, 0, 0, this);

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

private synchronized void stop() {

stop = true;

}

// 变量声明 - 不进行修改

private java.awt.Canvas screenCanvas;

// 变量声明结束

private volatile boolean stop;

private Robot robot;

private Dimension dim;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值