核心类: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() {
@Override
public 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 即可文章来源:https://www.toymoban.com/news/detail-648364.html
当桌面缩放与布局 设置125%时,需要使用计算后的位置坐标 X/125 ,Y/125文章来源地址https://www.toymoban.com/news/detail-648364.html
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 {
//时间间隔 1s
final 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,798
Danji(robot, 650, 880,1500);
//钉窗口文本输入鼠标点击位置坐标:551,399
Danji(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,891
Danji(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+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);
}
}
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() {
@Override
public 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);
}
}
到了这里,关于JAVA 鼠标控制与键盘输入控制的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!