Java鼠标自动点击及文件内容读取复制
程序员文章站
2024-01-10 08:58:28
...
为了避免某些重复工作,将固定的鼠标点击和内容复制工作用代码完成。
public class shubiao {
//时间间隔 1s
final static int DELAY=1000;
//文件路径
final static String path="C:\\Users\\dell\\OneDrive\\桌面\\wwj.txt";
//主方法
public static void main(String[] args) throws AWTException, IOException {
//robot对象,java自带
Robot rt=new Robot();
//获取文件
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String line=null;
while ((line=br.readLine())!= null){
String[] lin = line.split(",");
//移动到指定位置
Danji(rt,259,345);
//将文件内容复制到粘贴板,然后进行ctrl+v
outPut(lin[0],rt);
}
}
//工具类
//获取当前鼠标坐标
public static void sbAddress(Robot rt){
System.out.printf(MouseInfo.getPointerInfo().getLocation().getX()+"-------");//X轴
System.out.println(MouseInfo.getPointerInfo().getLocation().getY());//Y轴
System.out.printf(" ");
//间隔时间
//rt.delay(1000);
}
//鼠标移动到指定坐标,然后单击
public static void Danji(Robot rt,int x, int y){
rt.mouseMove(-1,-1);//初始化
rt.delay(DELAY);
rt.mouseMove(x,y);//制动到指定位置
rt.delay(DELAY);
pressMouse(rt, InputEvent.BUTTON1_MASK);
}
//鼠标点击事件
public static void pressMouse(Robot rt,int m){
rt.mousePress(m);
rt.delay(DELAY);
rt.mouseRelease(m);
rt.delay(DELAY);
}
//将指定内容粘贴到粘贴板 然后ctrl+v
public static void outPut(String str,Robot rt){
StringSelection selection = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection,selection);
keyPressWithCotrl(rt,KeyEvent.VK_V);
}
//ctrl组合键 相当于按下CTRL再按下传入的key值
public static void keyPressWithCotrl(Robot rt,int key){
rt.keyPress(KeyEvent.VK_CONTROL);
rt.keyPress(key);
rt.keyRelease(KeyEvent.VK_CONTROL);
rt.keyRelease(key);
rt.delay(DELAY);
}
}
上一篇: Mac终端中Vim编辑器快捷键
下一篇: 工厂模式