我想要一个键盘记录器,它通过按下一个键(任何键)来激活,然后将在设定的时间量(例如 10 秒)内按下的所有键保存在一个列表中,然后打印出来。我试过这个:
from pynput import keyboard
from time import sleep
def on_press(key):
global recording, keys_pressed
if not recording:
recording = True
sleep(10)
print(" ".join(keys_pressed))
recording = False
listener = keyboard.Listener(
on_press=on_press)
listener.start()
def on_release(key):
global recording
if recording:
print("key pressed")
global keys_pressed
Key = str(key)
if Key.startswith("Key."):
keys_pressed.append(Key[4:].strip("'"))
else:
keys_pressed.append(str(key).strip("'"))
listener2 = keyboard.Listener(
on_release=on_release)
listener2.start()
keys_pressed = []
recording = False
while True:
pass
它有点工作,但它多次打印我显然不想要的列表。该程序运行也非常缓慢。
如果您知道任何解决方案,请告诉我。