java实现升级版按键精灵

       现在一些手游可以在电脑模拟器实现操作,有一些无聊的副本手动去操作太浪费时间,网上的按键精灵功能太单一,按键时间固定又容易被识别,闲来无事写了一个java脚本实现按键时间周期设置,以及多个点的点击。

       在使用按键的时候java的窗口自动切换到后台,按键无法响应,为了实现窗口后台响应我这里需要导入一个第三方jar包jintellitype,具体代码如下

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * 这是窗口类,用来设置窗口
 * 
 * @author Stanley
 *
 */

public class KeyFrame extends JFrame implements Runnable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 初始提示
	private JLabel startLabel = new JLabel("鼠标初始位置");
	// 初始位置
	private JTextArea startArea = new JTextArea();
	// 第二次提示
	private JLabel secLabel = new JLabel("鼠标第二次点击");
	// 第二次确认位置
	private JTextArea secArea = new JTextArea();
	// 第三次提示
	private JLabel endLabel = new JLabel("鼠标最终位置");
	// 第三次确认位置
	private JTextArea endArea = new JTextArea();
	// 输入时间范围
	private JLabel timeLabel = new JLabel("请输入每个周期时间范围(s)");
	private JLabel space = new JLabel("");
	private JLabel minTimeLabel = new JLabel("请输入最小时间(s)");
	private JLabel maxTimeLabel = new JLabel("请输入最大时间(s)");
	private JLabel timeIntervalLabel = new JLabel("两次点击时间间隔(s)");
	private JLabel timesLabel = new JLabel("请输入周期次数");
	private JLabel lessTimes = new JLabel("剩余次数");
	// 最小时间
	private JTextField minTimeTextField = new JTextField();
	// 最大时间
	private JTextField maxTimeTextField = new JTextField();
	// 时间间隔
	private JTextField timeIntervalTextField = new JTextField();
	// 时间间隔
	private JTextField timesTextField = new JTextField();
	// 剩余次数
	private JTextArea lessTimeArea = new JTextArea();
	// 版权
	private JLabel author = new JLabel("Stanley Xu 制作");
	// 鼠标当前坐标
	double pointY = 0;
	double pointX = 0;
	// 鼠标起始坐标
	double startX = 0;
	double startY = 0;
	// 鼠标最终坐标
	double secX = 0;
	double secY = 0;
	// 鼠标最终坐标
	double endX = 0;
	double endY = 0;
	// 线程运行标记
	boolean autoFlag = false;
	// 设置时间范围
	double minTime = 1000;
	double maxTime = 2000;
	double timeInterval = 300;
	int n = 0;
	int i = 0;

	public void showPanel() {
		// 设置窗口大小
		this.setSize(500, 300);
		// 布置窗口内容
		JPanel panel = new JPanel();
		// 设置边框
		startArea.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
		secArea.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
		endArea.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
		lessTimeArea.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
		// 布置格式
		panel.setLayout(new GridLayout(13, 2));
		panel.add(startLabel);
		panel.add(startArea);
		// panel.add(startButton);
		panel.add(secLabel);
		panel.add(secArea);
		panel.add(endLabel);
		panel.add(endArea);
		panel.add(timeLabel);
		panel.add(space);
		panel.add(minTimeLabel);
		panel.add(minTimeTextField);
		panel.add(maxTimeLabel);
		panel.add(maxTimeTextField);
		panel.add(timeIntervalLabel);
		panel.add(timeIntervalTextField);
		panel.add(timesLabel);
		panel.add(timesTextField);
		panel.add(lessTimes);
		panel.add(lessTimeArea);
		// 布置操作提示
		JLabel metionLabel1 = new JLabel("F1确认鼠标初始位置");
		JLabel metionLabel2 = new JLabel("F2确认鼠标最终位置");
		JLabel metionLabel3 = new JLabel("F3确认鼠标最终位置");
		JLabel metionLabel4 = new JLabel("");
		JLabel metionLabel5 = new JLabel("F4开始结束 ");
		JLabel metionLabel6 = new JLabel("F5退出 ");
		JLabel metionLabel7 = new JLabel("F6最小化");
		JLabel metionLabel8 = new JLabel("F7恢复窗口");
		panel.add(metionLabel1);
		panel.add(metionLabel2);
		panel.add(metionLabel3);
		panel.add(metionLabel4);
		panel.add(metionLabel5);
		panel.add(metionLabel6);
		panel.add(metionLabel7);
		panel.add(metionLabel8);
		// 显示窗口
		JPanel authorPanel = new JPanel();
		authorPanel.setLayout(new FlowLayout());
		authorPanel.add(author);
		this.setLayout(new BorderLayout());
		this.add(panel, BorderLayout.CENTER);
		this.add(authorPanel, BorderLayout.SOUTH);
		// 增加键盘监听
		Thread t = new Thread(this);
		t.setPriority(Thread.MAX_PRIORITY);
		t.start();
		// 0表示F1,1表示F2,2表示F3,3表示F4,4表示F5,5表示F6,6表示F7
		JIntellitype.getInstance().registerHotKey(0, 0, 112);
		JIntellitype.getInstance().registerHotKey(1, 0, 113);
		JIntellitype.getInstance().registerHotKey(2, 0, 114);
		JIntellitype.getInstance().registerHotKey(3, 0, 115);
		JIntellitype.getInstance().registerHotKey(4, 0, 116);
		JIntellitype.getInstance().registerHotKey(5, 0, 117);
		JIntellitype.getInstance().registerHotKey(6, 0, 118);
		JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {
			@Override
			public void onHotKey(int arg0) {
				// TODO Auto-generated method stub
				Point mousePoint = MouseInfo.getPointerInfo().getLocation();
				pointX = mousePoint.getX();
				pointY = mousePoint.getY();
				// 确认起始坐标
				if (arg0 == 0) {
					startArea.setText("x:" + pointX + "  y:" + pointY);
					startX = pointX;
					startY = pointY;
					// 确认最终始坐标
				} else if (arg0 == 1) {
					secArea.setText("x:" + pointX + "  y:" + pointY);
					secX = pointX;
					secY = pointY;

				} else if (arg0 == 2) {
					endArea.setText("x:" + pointX + "  y:" + pointY);
					endX = pointX;
					endY = pointY;
				} else if (arg0 == 3 && autoFlag == false) {
					if (secX == 0 && secY == 0) {
						secX = startX;
						secY = startY;
					}
					if (endX == 0 && endY == 0) {
						endX = startX;
						endY = startY;
					}
					try {
						maxTime = Double.valueOf(maxTimeTextField.getText())* 1000;
						minTime = Double.valueOf(minTimeTextField.getText()) * 1000;
						timeInterval = Double.valueOf(timeIntervalTextField.getText()) * 1000;
						n = Integer.valueOf(timesTextField.getText());
					} catch (Exception e) {
						minTime = 1000;
						maxTime = 2000;
						timeInterval = 300;
						n = 10;
					}
					if ((startX == 0 && startY == 0)) {
						autoFlag = false;
					} else {
						autoFlag = true;
					}
					// 最小化
					// setExtendedState(JFrame.ICONIFIED);
					// autoFlag 为true,表示程序运行,按F4 暂停程序
				} else if (arg0 == 3&& autoFlag == true) {
					i = 0;
					autoFlag = false;
					// 窗口正常
					// setExtendedState(JFrame.NORMAL);
					// F5退出程序
				} else if (arg0 == 4) {
					System.exit(0);
				} else if (arg0 == 5) {
					setExtendedState(JFrame.ICONIFIED);
				} else if (arg0 == 6) {
					setExtendedState(JFrame.NORMAL);
				}
			}
		});
		setTitle("按键精灵");
		this.setVisible(true);
		// 窗口一直在上面
		this.setAlwaysOnTop(true);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			while (autoFlag && i <= n) {
				
				lessTimeArea.setText(n - i + "次");
				i++;
				double time = timeCal(minTime, maxTime);
				RobotOperation.mouseOp(startX, startY, secX,secY,endX, endY, timeInterval);
				try {
					Thread.sleep((long) time);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	// 计算时间范围
	public double timeCal(double minTime, double maxTime) {
		double timeDev = maxTime - minTime;
		double time =  (timeDev * (Math.random())) + minTime;
		return time;
	}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

/**
 * 鼠标自动控制类
 * 
 * @author Stanley
 *
 */

public class RobotOperation {

	public static void mouseOp(double startX, double startY, double secX,double secY, double endX, double endY,double timeInterval) {
		try {
			Robot rbt = new Robot();
			rbt.mouseMove((int) startX, (int) startY);// 移动到初始位置
			rbt.delay(100);// 休息0.1秒按鼠标
			rbt.mousePress(KeyEvent.BUTTON1_DOWN_MASK);// 按下鼠标
			// rbt.delay(1000);
			rbt.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);// 放开鼠标
			rbt.delay((int) timeInterval);// 休息间隔时间
			rbt.mouseMove((int) secX, (int) secY);
			rbt.delay(100);
			rbt.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
			// rbt.delay(1000);
			rbt.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
			rbt.delay((int) timeInterval);
			rbt.mouseMove((int) endX, (int) endY);
			rbt.delay(100);
			rbt.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
			// rbt.delay(1000);
			rbt.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
			rbt.delay((int) timeInterval);

		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
/**
 * 测试类
 * @author Stanley
 *
 */
public class Test {
	public static void main(String[] args) {
		KeyFrame kf = new KeyFrame();
		kf.showPanel();
	}
}

最终实现效果如下

java实现升级版按键精灵_第1张图片


你可能感兴趣的:(java)