核心類:java.awt.Robot
該類是JDK定義的電腦系統的抽象類,可以用來模擬實現鼠標點擊與鍵盤輸入等信息
簡單實現一個自動搶票代碼:
Robot rt = new Robot();//可以認為是操作間隔的停歇時間,比如等待頁面加載,等彈框內容展示等
rt.delay(1000);//鼠標移動到指定位置
rt.mouseMove(876,231);//點擊動作由下壓+回彈組成
//點擊按下去
rt.mousePress(InputEvent.BUTTON1_MASK);
rt.delay(100);
//放開回彈
rt.mouseRelease(InputEvent.BUTTON1_MASK);//copy黏貼文本
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
String[] authors = msgArr.get(getRandomIntNum()).split("[,]");
Transferable text = new StringSelection(authors[0]);
//將消息數組中的文本copy到粘貼板,使用ctrl+V即可完成粘貼
clip.setContents(text, null);//組合鍵使用ctrl+V,控制電腦完成粘貼動作
rt.keyPress(KeyEvent.VK_CONTROL);
rt.keyPress(KeyEvent.VK_V);//獲取鼠標坐標
Point point = java.awt.MouseInfo.getPointerInfo().getLocation();
System.out.println("" + point.x+" , "+point.y);//實際使用時配合線程調度
/**
* Launch the application.
*
* 實時獲取鼠標坐標
*/
public static void main(String[] args) {try {MouseInfo info_frame = new MouseInfo();info_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);info_frame.setVisible(true);info_frame.setAlwaysOnTop(true);Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {Point point = java.awt.MouseInfo.getPointerInfo().getLocation();System.out.println("" + point.x+" , "+point.y);try {Thread.sleep(3000L);} catch (InterruptedException e) {e.printStackTrace();}}}, 100, 100);} catch (Exception e) {e.printStackTrace();}
}
鼠標點擊模擬時 有三種按鍵類型:
InputEvent.BUTTON1_MASK 左鍵 (食指點擊)
InputEvent.BUTTON2_MASK 中鍵 (滾輪)
InputEvent.BUTTON3_MASK 右鍵(中指點擊)
WIN10 系統桌面坐標拾取
得到的信息需要根據屏幕--顯示設置--縮放與布局的百分比進行計算
當桌面縮放與布局 設置100%時,直接使用鼠標拾取的坐標 X,Y 即可
當桌面縮放與布局 設置125%時,需要使用計算后的位置坐標 X/125 ,Y/125
package com.test.domain;import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class Operation {//時間間隔 1sfinal static int DELAY=1000;private static List<String> msgArr=new ArrayList<String>();public static int getRandomIntNum() {int max=10;int min=1;Random random = new Random();return random.nextInt(max)%(max-min+1) + min;}/*** 用法簡介:腳本程序跑起來之后鼠標選中需要轟炸得聊天窗口,需要聊天窗口能獲取到鼠標得焦點** @param args* @throws AWTException*/public static void main1(String[] args) throws AWTException {//轟炸內容for (int i = 0; i < 10; i++) {String emoj="[炸彈] [炸彈] [炸彈] [炸彈] [炸彈] [炸彈] [炸彈][炸彈] [炸彈] ";msgArr.add("轟炸機,消息轟炸 ->"+i+emoj);}Robot robot = new Robot();robot.delay(5000);Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();String[] authors = msgArr.get(getRandomIntNum()).split("[,]");//循環轟炸for (int j = 0; j < 100; j++){for (int i = 0; i < authors.length; i++) {String str1 = authors[i];Transferable text = new StringSelection(str1);clip.setContents(text, null);robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.delay(100);robot.keyPress(KeyEvent.VK_ENTER);}}}/*** 用法簡介:腳本程序跑起來之前需要打開需要轟炸的對方聊天窗口,并全屏* 注:根據實際系統設置不同使用前需要將自己桌面縮放與布局大小調整為100%* 另需要用mouseInfo 工具類獲取到自己電腦上面發送 釘 消息的幾個操作按鈕坐標* 1.聊天窗口中的 DING一下按鈕 鼠標點擊坐標* 2.DING一下 窗口文本輸入框位置坐標* 3.DING一下 發送按鈕鼠標點擊坐標** @param args* @throws AWTException*/public static void main(String[] args) throws AWTException {//轟炸內容for (int i = 0; i < 10; i++) {String emoj = "[炸彈] [炸彈] [炸彈] [炸彈] [炸彈] [炸彈] [炸彈][炸彈] [炸彈] ";msgArr.add("轟炸機,消息轟炸 ->" + i + emoj);}Robot robot = new Robot();robot.delay(1000);//循環轟炸for (int j = 0; j < 3; j++) {robot.mouseMove(650, 880);Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();String[] authors = msgArr.get(getRandomIntNum()).split("[,]");//釘釘聊天框釘按鈕鼠標點擊位置坐標:819,798Danji(robot, 650, 880,1500);//釘窗口文本輸入鼠標點擊位置坐標:551,399Danji(robot, 1132, 466,100);Danji(robot, 1132, 466,100);Transferable text = new StringSelection(authors[0]);clip.setContents(text, null);robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.delay(1000);//釘消息發送按鈕鼠標點擊位置坐標:489,891Danji(robot, 576, 835,300);}}//鼠標移動到指定坐標,然后單擊public static void Danji(Robot rt,int x, int y,int times){rt.mouseMove(-1,-1);//初始化rt.delay(100);rt.mouseMove(x,y);//制動到指定位置rt.delay(100);pressMouse(rt, InputEvent.BUTTON1_MASK,times);}//鼠標點擊事件public static void pressMouse(Robot rt,int m,int times){rt.mousePress(m);rt.delay(100);rt.mouseRelease(m);rt.delay(times);System.out.printf("鼠標點擊完成"+m);}//將指定內容粘貼到粘貼板 然后ctrl+vpublic 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);}}
package com.test.domain;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Point;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Color;
public class MouseInfo extends JFrame {private final JPanel contentPanel = new JPanel();JLabel value_x = null;JLabel value_y = null;/*** Launch the application.** 實時獲取鼠標坐標*/public static void main(String[] args) {try {MouseInfo info_frame = new MouseInfo();info_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);info_frame.setVisible(true);info_frame.setAlwaysOnTop(true);Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {Point point = java.awt.MouseInfo.getPointerInfo().getLocation();System.out.println("" + point.x+" , "+point.y);try {Thread.sleep(3000L);} catch (InterruptedException e) {e.printStackTrace();}}}, 100, 100);} catch (Exception e) {e.printStackTrace();}}/*** Create the dialog.*/public MouseInfo() {setTitle("\u9F20\u6807\u5750\u6807\u83B7\u53D6\u5668");setBounds(100, 100, 217, 156);getContentPane().setLayout(new BorderLayout());contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));getContentPane().add(contentPanel, BorderLayout.CENTER);contentPanel.setLayout(null);JLabel lblx = new JLabel("\u5750\u6807x:");lblx.setFont(new Font("宋體", Font.PLAIN, 15));lblx.setBounds(22, 27, 66, 31);contentPanel.add(lblx);JLabel lbly = new JLabel("\u5750\u6807y:");lbly.setFont(new Font("宋體", Font.PLAIN, 15));lbly.setBounds(22, 68, 66, 31);contentPanel.add(lbly);value_x = new JLabel("0");value_x.setForeground(Color.BLUE);value_x.setFont(new Font("宋體", Font.PLAIN, 20));value_x.setBounds(82, 27, 66, 31);contentPanel.add(value_x);value_y = new JLabel("0");value_y.setForeground(Color.BLUE);value_y.setFont(new Font("宋體", Font.PLAIN, 20));value_y.setBounds(82, 68, 66, 31);contentPanel.add(value_y);}
}