我正在尝试编写一个程序,它可以从.txt文件中提取一个单词并按下键盘上的某个键。我这样做是为了让我的Twitch聊天工具可以通过命令控制我的键盘。下面是没有按键的代码(它不工作)。
with open('C:\Users\laith\AppData\Roaming\HexChat\logs\Twitch\#liongenz9629.log') as file:
contents = file.read()
search_word = input("test2")
if search_word in contents:
print ('word found')
else:
print ('word not found')我也不知道怎么按键。我尝试包含以下代码:
Send {F down}{Fup}
Send {F down}{Fup}要实现此目的,请执行以下操作:
with open('C:\Users\laith\AppData\Roaming\HexChat\logs\Twitch\#liongenz9629.log') as file:
contents = file.read()
search_word = input("test2")
if search_word in contents:
Send {F down}{Fup}
Send {F down}{Fup}
else:
print ('word not found')但我想知道的是,为什么它不起作用。我对它进行了广泛的研究,但找不到解决方案,所以我在这里……
我使用的是Python 2.7.18 64位
PC: Windows 10家庭版1903 Build 18362.959
发布于 2020-09-12 05:51:32
你试过pynput包了吗?它应该可以在python2.7上工作。
安装execute
pip install pynput文档中的示例:
from pynput.keyboard import Key, Controller
keyboard = Controller()
# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)
# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')
# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
keyboard.press('a')
keyboard.release('a')
# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')https://stackoverflow.com/questions/63854591
复制相似问题