我不太清楚该怎么回答这个问题,所以我会在这里解释我的问题。
我正在编写一个程序,该程序从txt文件中读取文本(例如文章),然后在键盘上键入一个字母时,从该txt文件中键入每个字母(想象一下那些将键盘混在一起的黑客游戏,它看起来像是在键入一些您不是的东西)。
目前,我正在处理这个简单删除字符后,但我注意到,我需要添加一个延迟,这与任何稳定的工作。0.05的延迟可以正常工作,但是任何较低的值都是不稳定的,0.05已经是我所喜欢的太多了。我也尝试过使用keyboard.press_and_release(),但这需要同样大的延迟而不中断。
我使用键盘模块,因为它同时工作在Windows和Mac上,这是必须具备的。我也不太清楚为什么会发生这种情况,特别是在新闻和发布功能方面,所以希望有人可能知道答案,或者使用不同的模块。我也尝试过pyautogui,这甚至更糟。
import pyautogui
import time
import keyboard
# only keyboard needs to be pip installed i think
if __name__ == '__main__':
keyboard.wait("ctrl")
time.sleep(2)
inFile = open('Essay', 'r')
while True:
line = inFile.readline();
# if line is empty meaning file is reached
if not line:
break
while len(line) > 0:
keyboard.read_key()
time.sleep(0.05)
keyboard.press("backspace")
time.sleep(0.05)
keyboard.press(line[0])
line = line[1:len(line)]
time.sleep(0.05)
keyboard.press("enter")发布于 2022-02-25 04:34:42
我不确定,但这可能对你有帮助。安装新的PyPI包:
pip install keyboard或者克隆存储库(不需要安装,源文件就足够了):
git clone https://github.com/boppreh/keyboard或者下载并解压到您的项目文件夹中。
然后检查下面的API文档以查看哪些特性可用。
示例:
import keyboard
keyboard.press_and_release('shift+s, space')
keyboard.write('The quick brown fox jumps over the lazy dog.')
keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))
# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))
# Blocks until you press esc.
keyboard.wait('esc')
# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')
# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)
# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@', 'my.long.email@example.com')
# Block forever, like `while True`.
keyboard.wait()礼貌: https://softans.com/how-to-replace-keyboard-output-using-python/
https://stackoverflow.com/questions/71260910
复制相似问题