吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2868|回复: 10
收起左侧

[其他转载] Python编写的自动点击与输入工具

  [复制链接]
DayTorches 发表于 2021-11-3 13:33

利用python实现将窗口拖动到(0,0)然后实现鼠标自动点击,和输入功能,编写配置文件实现用户自定义。

Main.py

import os
import time
import win32api
import win32con
import win32gui
import json
from pykeyboard import PyKeyboard
from pymouse import PyMouse

k = PyKeyboard()
m = PyMouse()

config_error = "配置文件有错误!!!!!!!"
windows_not_open_error = "请打开应用后在使用!!!!!!"

# 模拟鼠标点击
def mouse_click(x, y):
    win32api.SetCursorPos([x, y])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

# 输入信息
def type_string(context):
    k.type_string(context)

# 查找窗口,并设置位置
def find_window(window_name):
    # 获取句柄
    hwnd = win32gui.FindWindow(None, window_name)
    if hwnd == 0:
        return 0
    # 获取窗口左上角和右下角坐标
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    # 计算高度和宽度
    win_width = right - left
    win_height = bottom - top
    # 设置窗口位置为0,0
    win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, win_width, win_height, win32con.SWP_SHOWWINDOW)
    return hwnd

def config_application():
    print("正在使用配置模式")
    while True:
        time.sleep(0.1)
        print(f'\r当前鼠标位置:{m.position()}', end="")

def run(times, time_gap, operations):
    # 循环次数
    for i in range(0, times):
        print(f'正在进行第{i+1}次操作')
        # 执行操作
        for operation in operations:
            time.sleep(time_gap)
            operation_type = list(operation.keys())[0]
            # 点击操作
            if operation_type == "click":
                point = operation[operation_type]
                mouse_click(point[0], point[1])
            # 输入操作
            elif operation_type == "type_string":
                type_str_tmp = operation[operation_type]
                type_string(type_str_tmp)
            else:
                print(config_error)
                return

def init() -> None:
    # 直接从json文件中读取数据返回一个python对象
    data = ''
    try:
        f = open('config.json', encoding='utf-8')
        data = json.load(f)
    except:
        print("缺少配置文件!!!!")
        return
    title = data['title']
    if title is None:
        print(config_error)
    hwnd = find_window(title)
    if hwnd == 0:
        print(windows_not_open_error)
        return
    print("请选择需要的功能:\n1. 配置 \n2. 运行")
    input_type = input("> ")

    if input_type == "1":
        config_application()
    elif input_type == "2":
        run(data['time'], data['time_interval'], data['operation'])
    else:
        print("能不能正常输入???")

if __name__ == '__main__':
    init()
    os.system("pause")

配置文件

{
  "title": "xxx窗口名称",
  "time": 1,
  "time_interval": 0.5,
  "operation": [
    {
      "click": [357, 25]
    },
    {
      "click": [403,40]
    },
    {
      "click": [769,330]
    },
    {
      "type_string": "ylf"
    },
    {
      "click": [806, 408]
    },
    {
      "click": [1105, 748]
    }
  ]
}

免费评分

参与人数 5吾爱币 +4 热心值 +5 收起 理由
搬砖孩子要毕业 + 1 + 1 谢谢@Thanks!
brucekk + 1 热心回复!
愚鱼娱渔 + 1 + 1 用心讨论,共获提升!
cjcmxc + 1 + 1 谢谢@Thanks!
Zeaf + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

gunxsword 发表于 2021-11-3 17:41
感谢分享脚本!
2014晴天 发表于 2021-11-3 17:51
vethenc 发表于 2021-11-3 19:39
Cacarot 发表于 2021-11-3 22:26
按键精灵么?
cxyhahaha 发表于 2021-11-4 10:54
收藏学习一下
 楼主| DayTorches 发表于 2021-11-5 09:14

就自己写的,帮朋友能自动点击按钮转发,扩展了一下可以自定义。
FFEE 发表于 2021-11-5 10:18
这个对于初学者是很好的思维训练。
nbker 发表于 2021-11-8 00:40
恭喜分享
124544802 发表于 2021-12-1 15:51
感谢分享,收到
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 06:08

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表