Files
luoluo/demo_input.py

276 lines
7.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
InputSimulator 使用示例
演示如何使用输入模拟器进行鼠标点击、滑动和键盘操作
运行前请确保:
1. 游戏《桃源深处有人家》已启动
2. 游戏窗口可见(未被最小化)
3. 以管理员权限运行此脚本
"""
import sys
import time
from pathlib import Path
# 添加项目根目录到路径
sys.path.insert(0, str(Path(__file__).parent))
from src.core.game_window import GameWindowManager
from src.core.input_simulator import InputSimulator
def demo_basic_click():
"""演示基础点击"""
print("\n" + "="*50)
print("演示1: 基础点击")
print("="*50)
# 捕获游戏窗口
window = GameWindowManager()
if not window.capture_window():
print("❌ 未找到游戏窗口,请确保游戏已运行")
return False
print(f"✅ 已捕获窗口: {window.client_size}")
# 创建模拟器
sim = InputSimulator(window)
# 获取窗口中心坐标
size = window.client_size
center_x = size[0] // 2
center_y = size[1] // 2
print(f"\n将在窗口中心 ({center_x}, {center_y}) 进行点击演示")
print("3秒后开始...")
time.sleep(3)
# 1. 短按点击
print("\n1. 短按点击...")
sim.click(center_x, center_y)
time.sleep(1)
# 2. 长按1秒
print("2. 长按1秒...")
sim.click(center_x, center_y, duration=1.0)
time.sleep(1)
# 3. 双击
print("3. 双击...")
sim.double_click(center_x, center_y)
time.sleep(1)
print("✅ 点击演示完成")
return True
def demo_swipe():
"""演示滑动操作"""
print("\n" + "="*50)
print("演示2: 滑动操作")
print("="*50)
window = GameWindowManager()
if not window.capture_window():
print("❌ 未找到游戏窗口")
return False
sim = InputSimulator(window)
size = window.client_size
# 计算滑动起点和终点
start_x = size[0] // 4
start_y = size[1] // 2
end_x = size[0] * 3 // 4
end_y = size[1] // 2
print(f"\n将从 ({start_x}, {start_y}) 滑动到 ({end_x}, {end_y})")
print("3秒后开始...")
time.sleep(3)
# 水平滑动
print("\n1. 水平滑动0.5秒)...")
sim.swipe(start_x, start_y, end_x, end_y, duration=0.5)
time.sleep(1)
# 垂直滑动
print("2. 垂直滑动0.5秒)...")
sim.swipe(size[0] // 2, size[1] // 4, size[0] // 2, size[1] * 3 // 4, duration=0.5)
time.sleep(1)
# 对角线滑动
print("3. 对角线滑动1秒...")
sim.swipe(100, 100, size[0]-100, size[1]-100, duration=1.0)
time.sleep(1)
print("✅ 滑动演示完成")
return True
def demo_keyboard():
"""演示键盘操作"""
print("\n" + "="*50)
print("演示3: 键盘操作")
print("="*50)
window = GameWindowManager()
if not window.capture_window():
print("❌ 未找到游戏窗口")
return False
sim = InputSimulator(window)
print("\n3秒后开始键盘演示...")
print("注意:请确保游戏窗口可以接收键盘输入")
time.sleep(3)
# 1. 按ESC键通常用于关闭菜单
print("\n1. 按 ESC 键...")
sim.key_press('esc')
time.sleep(1)
# 2. 按空格键
print("2. 按空格键...")
sim.key_press('space')
time.sleep(1)
# 3. 组合按键示例按住Ctrl再按A
print("3. 组合按键 Ctrl+A...")
sim.key_down('ctrl')
sim.key_press('a')
sim.key_up('ctrl')
time.sleep(1)
print("✅ 键盘演示完成")
return True
def demo_game_automation():
"""
演示游戏自动化场景
模拟一个简单的游戏操作流程
"""
print("\n" + "="*50)
print("演示4: 游戏自动化场景")
print("="*50)
window = GameWindowManager()
if not window.capture_window():
print("❌ 未找到游戏窗口")
return False
sim = InputSimulator(window)
size = window.client_size
print("\n这是一个模拟的游戏自动化流程:")
print("1. 打开菜单(点击菜单按钮)")
print("2. 选择选项(滑动选择)")
print("3. 确认(点击确认按钮)")
print("4. 关闭菜单按ESC")
print("\n5秒后开始自动化流程...")
time.sleep(5)
# 步骤1: 点击菜单按钮(假设在右上角)
print("\n步骤1: 点击菜单按钮...")
menu_x = size[0] - 100
menu_y = 100
sim.click(menu_x, menu_y)
time.sleep(1)
# 步骤2: 滑动选择选项
print("步骤2: 滑动选择选项...")
sim.swipe(size[0]//2, size[1]//2, size[0]//2, size[1]//2 - 200, duration=0.3)
time.sleep(1)
# 步骤3: 点击确认按钮
print("步骤3: 点击确认按钮...")
confirm_x = size[0] // 2
confirm_y = size[1] - 150
sim.click(confirm_x, confirm_y)
time.sleep(1)
# 步骤4: 关闭菜单
print("步骤4: 关闭菜单...")
sim.key_press('esc')
time.sleep(1)
print("✅ 自动化流程完成")
return True
def check_admin():
"""检查是否以管理员权限运行"""
import ctypes
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def main():
"""主函数"""
print("="*50)
print("InputSimulator 使用示例")
print("="*50)
print("\n请确保游戏《桃源深处有人家》已启动")
print("游戏窗口需要可见(未被最小化)")
# 检查管理员权限
if not check_admin():
print("\n⚠️ 警告:未以管理员权限运行")
print("某些功能可能无法正常工作")
print("建议:右键点击命令提示符/终端,选择'以管理员身份运行'")
print()
response = input("是否继续运行?(y/n): ").strip().lower()
if response != 'y':
print("程序已退出")
return
# 显示菜单
while True:
print("\n" + "="*50)
print("请选择要运行的演示:")
print("="*50)
print("1. 基础点击演示")
print("2. 滑动操作演示")
print("3. 键盘操作演示")
print("4. 游戏自动化场景演示")
print("5. 运行全部演示")
print("0. 退出")
choice = input("\n请输入选项 (0-5): ").strip()
if choice == '0':
print("\n感谢使用,再见!")
break
elif choice == '1':
demo_basic_click()
elif choice == '2':
demo_swipe()
elif choice == '3':
demo_keyboard()
elif choice == '4':
demo_game_automation()
elif choice == '5':
demo_basic_click()
demo_swipe()
demo_keyboard()
demo_game_automation()
print("\n" + "="*50)
print("✅ 所有演示已完成")
print("="*50)
else:
print("\n❌ 无效选项,请重新输入")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n程序被用户中断")
except Exception as e:
print(f"\n❌ 发生错误: {e}")
import traceback
traceback.print_exc()