1. 程式人生 > >java實現升級版按鍵精靈

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();
	}
}

最終實現效果如下