本文介绍一个基于python的windows键盘鼠标自动化模拟器,适合自动化测试、游戏辅助等场景。代码通过ctypes调用windows api实现底层输入模拟。
实测案例:bongo cat刷点击

1. 主要功能
模拟键盘按键(按下、释放、完整按键)
模拟鼠标点击(左键、右键,可指定坐标)
支持绝对坐标定位
可自定义点击间隔和范围
2. 代码结构
2.1 inputsimulator类
负责封装所有输入模拟相关方法。
class inputsimulator:
def __init__(self):
self.user32 = ctypes.windll('user32', use_last_error=true)
# ... 定义各种常量 ...
主要方法
press_key(key_code):模拟按键按下release_key(key_code):模拟按键释放key_press(key_code, duration=0.1):完整按键(按下+延时+释放)click_mouse(x=none, y=none):模拟鼠标左键点击(可指定坐标)right_click_mouse(x=none, y=none):模拟鼠标右键点击(可指定坐标)
2.2 结构体定义
使用ctypes定义windows api所需的结构体:
class mouseinput(ctypes.structure):
_fields_ = (
("dx", wintypes.long),
("dy", wintypes.long),
("mousedata", wintypes.dword),
("dwflags", wintypes.dword),
("time", wintypes.dword),
("dwextrainfo", ctypes.pointer(wintypes.ulong))
)
# 还有 keybdinput 和 input 结构体
2.3 示例用法
主程序部分演示了如何自动点击:
if __name__ == "__main__":
import keyboard
import random
simulator = inputsimulator()
print("程序已启动,按q键退出...")
while true:
if keyboard.is_pressed('q'):
print("检测到q键,程序退出")
break
x = random.randint(1000, 1100)
y = random.randint(700, 800)
simulator.click_mouse(x, y)
time.sleep(random.uniform(0.05, 0.1))
- 随机生成点击坐标
- 按q键可随时退出
3. 技术要点
ctypes调用user32.dll:实现底层输入模拟
支持绝对坐标:通过65535缩放映射屏幕坐标
结构体封装:与windows api参数完全兼容
可扩展性强:可根据需要添加更多输入事件
4. 注意事项
仅支持windows系统
需管理员权限
请勿在重要操作界面误用
5. 应用场景
- 自动化测试
- 游戏脚本
- 重复性办公自动化
- 系统集成测试
完整代码见下方:
import ctypes
import time
from ctypes import wintypes
class inputsimulator:
def __init__(self):
self.user32 = ctypes.windll('user32', use_last_error=true)
# windows api 常量
self.input_mouse = 0
self.input_keyboard = 1
self.keyeventf_keyup = 0x0002
self.keyeventf_unicode = 0x0004
self.mouseeventf_leftdown = 0x0002
self.mouseeventf_leftup = 0x0004
self.mouseeventf_rightdown = 0x0008
self.mouseeventf_rightup = 0x0010
self.mouseeventf_move = 0x0001
self.mouseeventf_absolute = 0x8000
def press_key(self, key_code):
"""模拟按键按下"""
x = input(type=self.input_keyboard,
ki=keybdinput(wvk=key_code))
self.user32.sendinput(1, ctypes.byref(x), ctypes.sizeof(x))
def release_key(self, key_code):
"""模拟按键释放"""
x = input(type=self.input_keyboard,
ki=keybdinput(wvk=key_code,
dwflags=self.keyeventf_keyup))
self.user32.sendinput(1, ctypes.byref(x), ctypes.sizeof(x))
def key_press(self, key_code, duration=0.1):
"""模拟完整的按键过程"""
self.press_key(key_code)
time.sleep(duration)
self.release_key(key_code)
def click_mouse(self, x=none, y=none):
"""模拟鼠标点击
参数:
x: 鼠标x坐标(可选)
y: 鼠标y坐标(可选)
"""
if x is not none and y is not none:
screen_width = self.user32.getsystemmetrics(0)
screen_height = self.user32.getsystemmetrics(1)
x = int(x * 65535 / screen_width)
y = int(y * 65535 / screen_height)
move = input(type=self.input_mouse,
mi=mouseinput(dx=x, dy=y,
dwflags=self.mouseeventf_move | self.mouseeventf_absolute))
self.user32.sendinput(1, ctypes.byref(move), ctypes.sizeof(move))
time.sleep(0.1)
down = input(type=self.input_mouse,
mi=mouseinput(dwflags=self.mouseeventf_leftdown))
self.user32.sendinput(1, ctypes.byref(down), ctypes.sizeof(down))
time.sleep(0.1)
up = input(type=self.input_mouse,
mi=mouseinput(dwflags=self.mouseeventf_leftup))
self.user32.sendinput(1, ctypes.byref(up), ctypes.sizeof(up))
def right_click_mouse(self, x=none, y=none):
"""模拟鼠标右键点击"""
if x is not none and y is not none:
screen_width = self.user32.getsystemmetrics(0)
screen_height = self.user32.getsystemmetrics(1)
x = int(x * 65535 / screen_width)
y = int(y * 65535 / screen_height)
move = input(type=self.input_mouse,
mi=mouseinput(dx=x, dy=y,
dwflags=self.mouseeventf_move | self.mouseeventf_absolute))
self.user32.sendinput(1, ctypes.byref(move), ctypes.sizeof(move))
time.sleep(0.1)
down = input(type=self.input_mouse,
mi=mouseinput(dwflags=self.mouseeventf_rightdown))
self.user32.sendinput(1, ctypes.byref(down), ctypes.sizeof(down))
time.sleep(0.1)
up = input(type=self.input_mouse,
mi=mouseinput(dwflags=self.mouseeventf_rightup))
self.user32.sendinput(1, ctypes.byref(up), ctypes.sizeof(up))
# 保持原有的结构体定义
class mouseinput(ctypes.structure):
_fields_ = (
("dx", wintypes.long),
("dy", wintypes.long),
("mousedata", wintypes.dword),
("dwflags", wintypes.dword),
("time", wintypes.dword),
("dwextrainfo", ctypes.pointer(wintypes.ulong))
)
class keybdinput(ctypes.structure):
_fields_ = (
("wvk", wintypes.word),
("wscan", wintypes.word),
("dwflags", wintypes.dword),
("time", wintypes.dword),
("dwextrainfo", ctypes.pointer(wintypes.ulong))
)
class input(ctypes.structure):
class _input(ctypes.union):
_fields_ = (
("mi", mouseinput),
("ki", keybdinput)
)
_anonymous_ = ("_input",)
_fields_ = (
("type", wintypes.dword),
("_input", _input)
)
# 使用示例
if __name__ == "__main__":
"""
# 创建模拟器实例
simulator = inputsimulator()
# 示例:模拟按下a键
simulator.key_press(0x41)
# 示例:模拟鼠标点击
simulator.click_mouse()
# 示例:在指定位置右键点击
simulator.right_click_mouse(100, 100)
"""
import keyboard
import random
simulator = inputsimulator()
print("程序已启动,按q键退出...")
while true:
if keyboard.is_pressed('q'):
print("检测到q键,程序退出")
break
# 随机坐标(在1000-1100和700-800范围内)
x = random.randint(1000, 1100)
y = random.randint(700, 800)
# # 模拟鼠标右键点击
# simulator.right_click_mouse(x, y)
# time.sleep(random.uniform(0.05, 0.1))
# 模拟鼠标左键点击
simulator.click_mouse(x, y)
time.sleep(random.uniform(0.05, 0.1))
到此这篇关于python实现windows键盘鼠标自动化模拟器的文章就介绍到这了,更多相关python模拟键盘鼠标操作内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论