使用Java模拟鼠标重复点击

来源:互联网 发布:vs创建c语言程序 编辑:程序博客网 时间:2024/06/05 19:13

今天闲来无事,玩了一会小游戏发现游戏中有许多需要鼠标重复点击的操作再加上以前也听说过java可以模拟鼠标点击,所以研究了一个小时左右,自己写了一个小程序。

思路比较清晰,即先获取当前鼠标点击的坐标,然后再进行模拟鼠标点击。获取鼠标点击坐标在网上有许多是关于swing可视化界面的,这个地方花了比较长的时间。模拟鼠标点击自然就是使用robot。前台界面用的swing,逻辑方面使用了线程与定时器。


不依靠可视化界面直接获取鼠标坐标

Point point = MouseInfo.getPointerInfo().getLocation();System.out.println("x=" + point.getX() + ",y="+ point.getY());

robot简述

//初始化robotRobot robot = null;try {    robot = new Robot();} catch (AWTException e) {    e.printStackTrace();}//鼠标移动到某一点robot.mouseMove(x, y);//模拟鼠标按下左键robot.mousePress(InputEvent.BUTTON1_MASK);//模拟鼠标松开左键robot.mouseRelease(InputEvent.BUTTON1_MASK);//InputEvent.BUTTON2_MASK表示鼠标中键//InputEvent.BUTTON3_MASK表示鼠标右键//robot还可以模拟键盘点击,如有需要请自行百度

全部代码

package main;import java.awt.AWTException;import java.awt.MouseInfo;import java.awt.Point;import java.awt.Robot;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.util.Timer;import java.util.TimerTask;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class Main1 {    static double x = -1;    static double y = -1;    static boolean ifRun = true;    static JButton startBt;    static JButton endBt;    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        //显示主界面        showMain();    }    static Thread thread = new Thread(new Runnable() {        @Override        public void run() {            // TODO Auto-generated method            Robot robot = null;            try {                robot = new Robot();            } catch (AWTException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            //模拟移动到当前鼠标位置            robot.mouseMove((int) x, (int) y);            while (ifRun) {                try {                    thread.sleep(100);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                //模拟鼠标按下左键                robot.mousePress(InputEvent.BUTTON1_MASK);                //模拟鼠标松开左键                robot.mouseRelease(InputEvent.BUTTON1_MASK);            }        }    });    static public void showMsg(String msg) {        JOptionPane.showMessageDialog(null, msg, "提示信息",                JOptionPane.PLAIN_MESSAGE);    }    static public void showMain() {        JDialog dialog = new JDialog();        // 设置大小        dialog.setSize(200, 100);        // 设置标题        dialog.setTitle("界面");        startBt = new JButton("开始");        endBt = new JButton("结束");        //绑定监听        startBt.addActionListener(actionListener);        endBt.addActionListener(actionListener);        startBt.setBounds(35, 10, 60, 40);        endBt.setBounds(90, 10, 60, 40);        // 设置布局为空,使用坐标控制控件位置的时候,一定要设置布局为空        dialog.setLayout(null);        // 添加控件        dialog.add(startBt);        dialog.add(endBt);        // 设置dislog的相对位置,参数为null,即显示在屏幕中间        dialog.setLocationRelativeTo(null);        // 设置当用户在此对话框上启动 "close" 时默认执行的操作        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        // 设置是否显示        dialog.setVisible(true);    }    static ActionListener actionListener = new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) {            if (e.getSource() == startBt) {                showMsg("已开始——————请在三秒内点击");                new Timer().schedule(new TimerTask() {                    @Override                    public void run() {                         Point point = MouseInfo.getPointerInfo().getLocation();                        System.out.println("x=" + point.getX() + ",y="                                + point.getY());                        //获取当前鼠标的位置                        x = point.getX();                        y = point.getY();                        if (x != (-1) && y != (-1)) {                            showMsg("已开始获取鼠标位置并已启动线程");                            thread.start();                        } else {                            System.out.println("未获取到鼠标位置");                        }                    }                }, 3000);            }            if (e.getSource() == endBt) {                ifRun = false;                showMsg("结束");            }        }    };}

上段代码我一直有个疑问,schedule方法应该执行一次就不再执行了,可是在上段代码中却重复执行,因为x和y一直在刷新。怀疑是不是与线程相关。

另一种代码如下:

package main;import java.awt.AWTException;import java.awt.MouseInfo;import java.awt.Point;import java.awt.Robot;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.util.Timer;import java.util.TimerTask;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JOptionPane;public class Main1 {    static boolean ifRun = true;    static JButton startBt;    static JButton endBt;    public static void main(String[] args) {        // 显示主界面        showMain();    }    static Thread thread = new Thread(new Runnable() {        @Override        public void run() {            Robot robot = null;            try {                robot = new Robot();            } catch (AWTException e) {                e.printStackTrace();            }            // 模拟移动到当前鼠标位置            while (ifRun) {                Point point = MouseInfo.getPointerInfo().getLocation();                System.out.println("x=" + point.getX() + ",y=" + point.getY());                robot.mouseMove((int) point.getX(), (int) point.getY());                // 模拟鼠标按下左键                robot.mousePress(InputEvent.BUTTON1_MASK);                // 模拟鼠标松开左键                robot.mouseRelease(InputEvent.BUTTON1_MASK);                try {                    thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    });    static public void showMsg(String msg) {        JOptionPane.showMessageDialog(null, msg, "提示信息",                JOptionPane.PLAIN_MESSAGE);    }    static public void showMain() {        JDialog dialog = new JDialog();        // 设置大小        dialog.setSize(200, 100);        // 设置标题        dialog.setTitle("界面");        startBt = new JButton("开始");        endBt = new JButton("结束");        // 绑定监听        startBt.addActionListener(actionListener);        endBt.addActionListener(actionListener);        startBt.setBounds(35, 10, 60, 40);        endBt.setBounds(90, 10, 60, 40);        // 设置布局为空,使用坐标控制控件位置的时候,一定要设置布局为空        dialog.setLayout(null);        // 添加控件        dialog.add(startBt);        dialog.add(endBt);        // 设置dislog的相对位置,参数为null,即显示在屏幕中间        dialog.setLocationRelativeTo(null);        // 设置当用户在此对话框上启动 "close" 时默认执行的操作        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        // 设置是否显示        dialog.setVisible(true);    }    static ActionListener actionListener = new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) {            if (e.getSource() == startBt) {                showMsg("已开始——————请在三秒内点击");                new Timer().schedule(new TimerTask() {                    @Override                    public void run() {                        showMsg("已开始获取鼠标位置并已启动线程");                        thread.start();                    }                }, 3000);            }            if (e.getSource() == endBt) {                ifRun = false;                showMsg("结束");            }        }    };}

参考项目下载地址:
http://download.csdn.net/detail/zhengyikuangge/9831683
欢迎下载~

0 0
原创粉丝点击