疫情,需要远程办公,为了更好的远程办(划)公(水)。而我们公司因为没有想到会有大批量的远程办公,从而导致连接的人过多,需要抢占连接才能登录,而且好不容易抢到了,去上个厕所,然后就长时间未操作断开了,防止这种事情的发生,特地写了这个脚本
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;/*** Java实现鼠标随机移动*/
public class MouseController implements Runnable {private Robot robot;private boolean isStop = false;public MouseController() {try {ControllerFrame frame = new ControllerFrame("Prevent Locking");frame.setVisible(true);robot = new Robot();} catch (AWTException e) {e.printStackTrace();}}@Overridepublic void run() {int x;int y;Random random = new Random();while (!isStop) {//随机生成坐标。x = random.nextInt(10)+1000; // 1000y = random.nextInt(10)+600; //1000//开始移动鼠标robot.mouseMove(x, y);// robot.mousePress(KeyEvent.BUTTON3_DOWN_MASK); // 模拟按下鼠标右键
// robot.mouseRelease(KeyEvent.BUTTON3_DOWN_MASK); // 模拟释放鼠标右键//鼠标点击//robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);//鼠标抬起//robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);//每5秒一次操作robot.delay(5000);}}/*** GUI Frame 生成一个button,控制程序** @author max*/private class ControllerFrame extends JFrame {private static final long serialVersionUID = 1L;private JButton close = new JButton("close");public ControllerFrame(String title) {this();setTitle(title);}public ControllerFrame() {setLayout(new FlowLayout(FlowLayout.LEADING));setSize(100, 100);setResizable(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);Dimension preferredSize = new Dimension(100, 60);Font font = new Font("", 1, 14);//设置button 大小,文字等属性close.setPreferredSize(preferredSize);close.setFont(font);close.setBorderPainted(true);close.setFocusable(false);add(close);//点击button后,程序终止。close.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {isStop = true;dispose();}});}}public static void main(String[] args) {MouseController m = new MouseController();m.run();}}
运行后会弹出一个框,然后你就切换到会过期的应用窗口就行了
如果不想让鼠标继续动了那么点击close 就行了 ,以上案例中提供了鼠标左键和右键的操作,如果有需要自行改代码,都有注释