首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用python模拟应用程序中的按键

使用python模拟应用程序中的按键
EN

Stack Overflow用户
提问于 2022-03-28 04:26:58
回答 1查看 363关注 0票数 1

我正在尝试编写一个python脚本,它将从文本文档中键入行,就像它们来自键盘一样。我已经在一些应用程序中使用了一个代码片段(请参见下面),并且这将正确地键入我打开的文件中的每一行,例如,我将输出测试到notepad++中,并将其全部输入。

代码语言:javascript
运行
复制
import keyboard
import time

time.sleep(3) """ this gives me enough time to alt-tab into the game (Witcher 3) 
that I am trying to have the keypresses inserted into, I also tried some code with 
win32gui that brough the Witcher 3 app to the front, but this is simpler. """

with open('w3recipes.txt', 'r', encoding='utf-8') as recipes:
    for line in recipes:
        keyboard.write(line)
        time.sleep(0.05)

问题是这些击键不是由巫婆3注册的,我正在尝试写所有这些击键的游戏。我试着把游戏从全屏切换到窗口,没有运气,我试着把脚本编译成一个.exe,并将它作为管理员运行,没有骰子。我还尝试了pynput库,而不是这里使用的键盘库,这产生了同样的结果。

任何帮助都将不胜感激,我试图写几百个控制台命令到这个游戏中,在游戏的控制台中没有换行符,它一次只支持一个命令,然后点击enter。我唯一的选择是坐在这里复制粘贴所有的行,这将是令人厌烦的。

提前谢谢。

EN

Stack Overflow用户

发布于 2022-03-28 04:50:52

使用另一个库,pydirectinput。这是pyautogui的更新版本,我发现它适用于大多数(如果不是全部)游戏。

引用文档:

这个库的目的是复制PyAutoGUI鼠标和键盘输入的功能,但是使用DirectInput扫描代码和更现代的SendInput() win32函数。PyAutoGUI使用虚拟密钥代码(VKs)和不推荐的mouse_event()和keybd_event() win32函数。您可能会发现PyAutoGUI在某些应用程序中不起作用,特别是在电子游戏和其他依赖于DirectX的软件中。如果你发现自己在这种情况下,给这个图书馆一个尝试!

编写功能:

代码语言:javascript
运行
复制
>>> import pyautogui
>>> import pydirectinput
>>> pydirectinput.moveTo(100, 150) # Move the mouse to the x, y coordinates 100, 150.
>>> pydirectinput.click() # Click the mouse at its current location.
>>> pydirectinput.click(200, 220) # Click the mouse at the x, y coordinates 200, 220.
>>> pydirectinput.move(None, 10)  # Move mouse 10 pixels down, that is, move the mouse relative to its current position.
>>> pydirectinput.doubleClick() # Double click the mouse at the
>>> pydirectinput.press('esc') # Simulate pressing the Escape key.
>>> pydirectinput.keyDown('shift')
>>> pydirectinput.keyUp('shift')
# And this is the one you want,
>>> pydirectinput.write('string') # Write string
>>> pydirectinput.typewrite("string")
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71642404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档