Java使用Robot操作键盘和鼠标

package com.yeetrack.robot;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
/**
* Created with IntelliJ IDEA.
* User: victor
* Date: 13-9-26
* Time: 上午10:03
*/
public class RobotTest
{
    public static void main(String[] args) throws AWTException,         InterruptedException, IOException
    {
 
        Robot robot = new Robot();
        //设置Robot产生一个动作后的休眠时间,否则执行过快
        robot.setAutoDelay(1000);
 
       //获取屏幕分辨率
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        System.out.println(d);
        Rectangle screenRect = new Rectangle(d);
        //截图
        BufferedImage bufferedImage =       robot.createScreenCapture(screenRect);
        //保存截图
        File file = new File("screenRect.png");
        ImageIO.write(bufferedImage, "png", file);
 
        //移动鼠标
        robot.mouseMove(500, 500);
 
        //点击鼠标
        //鼠标左键
        System.out.println("单击");
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
        //鼠标右键
        System.out.println("右击");
        robot.mousePress(InputEvent.BUTTON3_MASK);
        robot.mouseRelease(InputEvent.BUTTON3_MASK);
 
        //按下ESC,退出右键状态
        System.out.println("按下ESC");
        robot.keyPress(KeyEvent.VK_ESCAPE);
        robot.keyRelease(KeyEvent.VK_ESCAPE);
        //滚动鼠标滚轴
        System.out.println("滚轴");
        robot.mouseWheel(5);
 
        //按下Alt+TAB键
        robot.keyPress(KeyEvent.VK_ALT);
        for(int i=1;i<=2;i++)
        {
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
        }
        robot.keyRelease(KeyEvent.VK_ALT);
 
    }
}

转载于:https://blog.csdn.net/free0006/article/details/84524752?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-84524752-blog-114102634.pc_relevant_multi_platform_whitelistv4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-84524752-blog-114102634.pc_relevant_multi_platform_whitelistv4&utm_relevant_index=2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaRobot类详解 概述 概述 JavaRobot类位于java.awt.Robot,该类⽤于为测试⾃动化,⾃运⾏演⽰程序和其他需要控制⿏标和键盘的应⽤程序⽣成本机系统输⼊ 事件,Robot类的主要⽬的是便于Java平台实现⾃动测试。 Robot可以模拟⿏标和键盘的输⼊,相当于Java版的按键精灵。 常⽤⽅法 常⽤⽅法 1.创建实例 Robot robot = new Robot(); 2.延时函数 robot.delay(毫秒值); 3.⿏标按下 robot.mousePress(⿏标上的按键); //⿏标左键:InputEvent.BUTTON1_DOWN_MASK //⿏标中键:InputEvent.BUTTON2_DOWN_MASK //⿏标右键:InputEvent.BUTTON3_DOWN_MASK 4.⿏标释放 robot.mouseRelease(⿏标上的按键); //⿏标左键:InputEvent.BUTTON1_DOWN_MASK //⿏标中键:InputEvent.BUTTON2_DOWN_MASK //⿏标右键:InputEvent.BUTTON3_DOWN_MASK 5.⿏标移动 //⿏标移动到指定位置 robot.mouseMove(int x,int y); 6.⿏标滚轮滑动 //⿏标滚动(参数⼩于0,表⽰向上滚动;参数⼤于0,表⽰向下滚动) robot.mouseWheel(int wheelAmt); 7.键盘按下指定的键 //键盘按下指定的键-----keycode:键盘键值常量,定义在KeyEvent.VK_XXX 中 //KeyEvent.VK_WINDOWS:键盘上的windows键 //KeyEvent.VK_CONTROL:键盘上的ctrl键 //KeyEvent.VK_L:键盘上的L键 robot.keyPress(int keycode); 8.键盘释放指定的键 //键盘按下指定的键-----keycode:键盘键值常量,定义在KeyEvent.VK_XXX 中 //KeyEvent.VK_WINDOWS:键盘上的windows键 //KeyEvent.VK_CONTROL:键盘上的ctrl键 //KeyEvent.VK_L:键盘上的L键 robot.keyRelease(int keycode); 9.获取屏幕指定坐标处像素颜⾊ //获取指定坐标处的像素颜⾊ Color color=robot.getPixelColor(int x,int y); 10.截取指定区域的图像(截图功能) //获取指定矩形区域的图像(截图) BufferedImage bufferedimage=robot.createScreenCapture(Rectangle screenRect); ⽰例:截取指定矩形区域的图像,并保存到指定的路径 public static void main(String[] args) throws AWTException, IOException { Robot robot=new Robot(); //获取指定矩形区域的屏幕图像 BufferedImage bufferedImage=robot.createScreenCapture(new Rectangle(100,100,500,500)); File f=new File("D:\\save.jpg"); OutputStream os=new FileOutputStream(f); ImageIO.write(bufferedImage, "jpg", os); } ⽰例:在指定区域⾃动输⼊指定字符 robot.mouseMove(342, 626); robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); robot.delay(500); robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_L); robot.delay(500); robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); robot.delay(500); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.delay(500); robot.keyPress(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_E)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值