python 写的一个按键精灵工具 特别方便 喜欢拿去

程序可以每隔固定周期时间自动进行操作鼠标、键盘、输入文本等。

屏幕坐标我是用FastStoneCapture软件的“屏幕十字线”功能取的,很方便,其实方法很多,屏幕截图用画图软件也可以。

代码如下:

main.py #主程序

import win32api
import pyautogui
import time
import os
import subprocess
from load_conf import config
print("程序启动")
interval_interval=int(config["interval-interval"])
print("操作周期:%d 秒" % interval_interval)
while 1:
    time.sleep(interval_interval)
    mouse_enable=True if config["mouse-enable"]=="yes" else False
    key_enable=True if config["key-enable"]=="yes" else False
    if mouse_enable:
        print("准备鼠标操作")
        x=int(config["mouse-x"])
        y=int(config["mouse-y"])
        times=int(config["mouse-times"])
        interval=float(config["mouse-interval"])
        action=config["mouse-action"]
        for t in range(times):
            if action=="left":
                pyautogui.leftClick(x=x,y=y,interval=interval)
                print("鼠标左键,(%d,%d)" % (x,y))
            elif action=="right":
                pyautogui.rightClick(x=x, y=y, interval=interval)
                print("鼠标右键,(%d,%d)" % (x, y))
            elif action=="double":
                pyautogui.doubleClick(x=x, y=y, interval=interval)
                print("鼠标双击,(%d,%d)" % (x, y))
    if key_enable:
        print("准备键盘操作")
        word=config["key-word"]
        key=config["key-key"]
        if word:
            pyautogui.typewrite(word,0.2)
            print("键盘输入: %s" % word)
        if key:
            pyautogui.press(key)
            print("键盘操作: %s" % key)

load_conf.py #配置程序

import configparser
import os
cf = configparser.ConfigParser()
cf.read("conf",encoding='utf-8')
config = {
        "interval-interval": cf.get("interval", "interval"),
        "mouse-x": cf.get("mouse", "x"),
        "mouse-y": cf.get("mouse", "y"),
        "mouse-times": cf.get("mouse", "times"),
        "mouse-interval": cf.get("mouse", "interval"),
        "mouse-enable": cf.get("mouse", "enable"),
        "mouse-action": cf.get("mouse", "action"),
        "key-word": cf.get("key", "word"),
        "key-key": cf.get("key", "key"),
        "key-enable": cf.get("key", "enable"),
    }

conf #配置文件

[interval]
interval=10
#秒

[mouse]
x=896
y=743
times=1
interval=1
enable=yes
action=left
#left right double

[key]
word="test"
key=enter
enable=yes


版权声明:本文为chscomfaner原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。