package com.huimv.receive.timer.test;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;

public class GameController {

    private static Robot robot;

    static {
        try {
            robot = new Robot();
            robot.setAutoDelay(10); // 设置自动事件间隔
            wait(3000);
        } catch (AWTException e) {
            throw new RuntimeException("初始化Robot失败", e);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            runPresetScript();

        }
//        runPresetScript2();
    }

    private static void runPresetScript2() {
        wait(2000);
        for (int i = 0; i < 6000; i++) {
            tapKey(KeyEvent.VK_2);
        }

    }

    private static void runPresetScript() {
        System.out.println("执行改进版预定义脚本...");
        holdKey(KeyEvent.VK_P,300);
        wait(1000);
        tapKey(KeyEvent.VK_SPACE);
        wait(4000);
        // 新版脚本测试：
        holdKey(KeyEvent.VK_P, 400); // 正确保持2秒
        holdKey(KeyEvent.VK_L, 500);
        wait(800);
        holdKey(KeyEvent.VK_L, 1000);
        wait(500);
        tapKey(KeyEvent.VK_A);
        wait(500);
        tapKey(KeyEvent.VK_A);
        wait(1000);
        tapKey(KeyEvent.VK_ESCAPE);

        wait(2000);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        wait(500);
        // 模拟鼠标左键释放lllllllllllllllllllllllllllllll
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        wait(3 *1000);

    }

    // 改进的按键保持方法
    private static void holdKey(int keyCode, int duration) {
        System.out.printf("按住 %s 键 %d 毫秒%n", KeyEvent.getKeyText(keyCode), duration);

        long startTime = System.currentTimeMillis();
        robot.keyPress(keyCode);

        // 持续保持按键状态
        while (System.currentTimeMillis() - startTime < duration) {
            robot.delay(10); // 使用Robot内置延迟
            robot.keyPress(keyCode); // 持续发送按键信号
        }

        robot.keyRelease(keyCode);
        System.out.println("释放按键");

    }




    // 快速点击
    private static void tapKey(int keyCode) {
        robot.keyPress(keyCode);
        robot.keyRelease(keyCode);
    }

    // 等待方法
    private static void wait(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}