taojy123 / KeymouseGo

类似按键精灵的鼠标键盘录制和自动化操作 模拟点击和键入 | automate mouse clicks and keyboard input
http://taojy123.github.io/KeymouseGo
GNU General Public License v2.0
7.15k stars 1.04k forks source link

可以增加一个定时执行吗 #280

Open xianyum opened 9 months ago

ACEKILLER-YOU commented 7 months ago

都提供命令行执行方式了,自己做计划任务就行了吧

ReasonDuan commented 5 months ago

给你提供一个插件WaitExtension.py
支持指定时间戳等待, 到指定时间戳后则开始执行,脚本第二行添加:[10,"WT","time",1716347696],将时间戳改为自己想启动的时间戳
支持获取指定点颜色等待 在需要等待的行添加:[200,"WT","color wait",[960, 1220],[255, 255, 255]], 第4个参数代表屏幕上的点位置,第5各个参数代表指定点对应的rgb颜色,当屏幕中指定点的颜色值发生改变则继续执行后续操作。

from assets.plugins.Extension import *
import time
import datetime
from loguru import logger
import win32gui

class WaitExtension(Extension):
    def __init__(self, runtimes, speed, thd=None, swap=None):
        super().__init__(runtimes, speed, thd, swap)

    def onrunbefore(self, event, currentindex):
        if event.event_type == "WT":
            # [10,"WT","time",1716344132],
            if event.message == 'time':
                try:
                    start_timestamp = event.action
                    date = datetime.datetime.fromtimestamp(start_timestamp)
                    custom_format = "%Y-%m-%d %H:%M:%S"  # 自定义格式:年-月-日 时:分:秒
                    formatted_date = date.strftime(custom_format)
                    logger.info('Script start at %s' % formatted_date)
                    # 获取当前时间戳
                    current_timestamp = time.time()
                    # 计算需要等待的秒数
                    seconds_to_wait = max(0, start_timestamp - current_timestamp)
                    # 等待到达目标时间戳
                    time.sleep(seconds_to_wait)
                except Exception as e:
                    logger.warning('Time stamp format error.')
            elif event.message == 'color wait':
                # 如果为某个color时,则一直等待
                # [200, "WT", "color wait", [960, 1220], [255, 255, 255]],
                try:
                    logger.debug("Color wait: ", event.addon)
                    x, y = event.action
                    while True:
                        color_given = tuple(event.addon)
                        pixel_color = self.get_pixel_color(x,y)
                        if color_given == pixel_color:
                            time.sleep(0.2)
                        else:
                            break
                    logger.debug("Color changed, script continues to run.")
                except Exception as e:
                    logger.warning('Color wait error: ', e)
            else:
                logger.warning('Unknown time event:%s' % self.message)
            # 等待事件,已执行,后续不需要执行
            return False

        return True

    def get_pixel_color(self, x, y):
        # 获取屏幕设备上下文
        hdc = win32gui.GetDC(0)
        # 获取指定点的颜色值
        color = win32gui.GetPixel(hdc, x, y)
        # 释放设备上下文
        win32gui.ReleaseDC(0, hdc)
        r, g, b = color & 0xff, (color >> 8) & 0xff, (color >> 16) & 0xff
        # 返回颜色值
        return (r, g, b)