暗黑破坏神3 按键精灵 python

import threading
import pyautogui
import time
import keyboard
import random

pyautogui.FAILSAFE = False


class App(object):

    def __init__(self):
        # params
        self.loop_sign = False
        self.lock = threading.Lock()

        self.click_keys = "WR"
        self.press_keys = ""

        # handle
        self.watch_keyboard()

    def loop(self):
        self.lock.acquire()  # 同时只有一个运行
        while self.loop_sign:
            for key in self.press_keys:
                pyautogui.keyDown(key)
            for key in self.click_keys:
                pyautogui.press(key)
            interval = 300
            rand_weight = 0.85 + random.random() + 0.3
            time.sleep(interval * rand_weight / 1000)

        for key in self.press_keys:
            pyautogui.keyUp(key)
        self.lock.release()

    def switch(self, loop_sign: bool):
        self.loop_sign = loop_sign
        if not self.loop_sign:
            return
        threading.Thread(target=self.loop).start()

    def watch_keyboard(self):
        keyboard.hook(self.watch_hook)
        keyboard.wait()

    def watch_hook(self, x):
        if x.event_type == "down" and x.name == "`":
            return self.switch(not self.loop_sign)
        if x.event_type == "down" and x.name == "t":
            return self.switch(False)


if __name__ == '__main__':
    App()

你可能感兴趣的:(游戏,python,游戏程序)