java_Robot类

Robot类

前段时间发现一个很有意思的java类--Robot,该类的主要作用是用来模拟按键及鼠标功能等。

package com.lc.util;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class RobotUtil {
	
	/**
	 * 两个按键组合按下
	 * @param r Robot
	 * @param key1 按键1,按键该传什么值可以查看源码
	 * @param key2 按键2
	 */
	public void keyPresses2(Robot r, int key1, int key2) {
		r.keyPress(key1);
        r.keyPress(key2);
        r.keyRelease(key2);
        r.keyRelease(key1);
        r.delay(100);
	}
	
	/**
	 * 三个按键组合
	 * @param r
	 * @param key1
	 * @param key2
	 */
	public void keyPresses3(Robot r, int key1, int key2, int key3) {
		r.keyPress(key1);
        r.keyPress(key2);
        r.keyPress(key3);
        r.keyRelease(key3);
        r.keyRelease(key2);
        r.keyRelease(key1);
        r.delay(100);
	}

    //打印出字符串,忘了在那个博客上看到的了
   /* public void keyPressString(Robot r, String str){

        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//获取剪切板
        Transferable tText = new StringSelection(str);
        clip.setContents(tText, null); //设置剪切板内容
        keyPressWithCtrl(r, KeyEvent.VK_V);//粘贴
        r.delay(100);
    }*/
    
    /**
     * 按键按下
     * @param r Robot
     * @param key 按键
     * @param time 松开延迟时间
     */
    public void keyPress(Robot r,int key, int time){
    	
        r.keyPress(key);
        r.delay(time);
        r.keyRelease(key);       
    }
    
    /**
     * 鼠标点击
     * @param r Robot
     * @param clickTime 点击次数
     */
    public void mousePressAndRelease(Robot r) {
            //按下鼠标
    		r.mousePress(KeyEvent.BUTTON1_MASK);
            //松开鼠标,一按一松才是点击
    		r.mouseRelease(KeyEvent.BUTTON1_MASK); 
    	 	
    }
    
    /**
     * 鼠标移动双击
     * @param r Robot
     * @param x 横坐标
     * @param y 纵坐标
     */
    public void mouseClick2(Robot rb, int x, int y) {
    	rb.mouseMove(x, y);
		rb.delay(500);
		mousePressAndRelease(rb);
		mousePressAndRelease(rb);
    }
    
    /**
     * 拖动鼠标
     * @param r Robot
     * @param x1 横坐标
     * @param y1 纵坐标
     * @param x2 横坐标
     * @param y2 纵坐标
     */
    public void mouseTuoDong(Robot rb, int x1, int y1, int x2, int y2) {
    	rb.mouseMove(x1, y1);
    	rb.mousePress(KeyEvent.BUTTON1_MASK);
		rb.delay(1000);
		rb.mouseMove(x2, y2);
		rb.mouseRelease(KeyEvent.BUTTON1_MASK);
    }
    
    /**
     * 鼠标移动单击
     * @param r Robot
     * @param x 横坐标
     * @param y 纵坐标
     */
    public void mouseClick1(Robot rb, int x, int y) {
    	rb.mouseMove(x, y);
		mousePressAndRelease(rb);
    }
    
    /**
     * 得到屏幕某点像素值
     * @param x 横坐标
     * @param y 纵坐标
     * @return
     */
    public int getScreenPixel(int x, int y) { // 函数返回值为颜色的RGB值。
		Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
		try {
			rb = new Robot();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
        //这个地方是传入屏幕的四个点,相当于截屏大小
		Rectangle rec = new Rectangle(x1, y1, x2, y2);
		BufferedImage bi = rb.createScreenCapture(rec);
		int pixelColor = bi.getRGB(x, y);
		//颜色值
		int pixelNum = 16777216 + pixelColor;
		int rgb[] = new int[3];
		//转化成rgb值
		rgb[0] = (pixelNum & 0xff0000) >> 16;
		rgb[1] = (pixelNum & 0xff00) >> 8;
		rgb[2] = (pixelNum & 0xff);
		return pixelNum; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
    }

}

可以用来实现简单的模拟屏幕鼠标按键自动化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值