def on_press(key, file_folder):
if key == keyboard.Key.ctrl_l:
print('ctrl')
with open(file_folder+"/按键日志.txt", "a", encoding='utf-8') as f:
f.write("左Ctrl\n")
else:
with open(file_folder+"/按键日志.txt", "a", encoding='utf-8') as f:
f.write(str(key) + "\n")
在这段代码中,使用 open 函数打开文件时,指定了 encoding='utf-8' 参数。这样就可以以utf-8编码写入文件。
def on_press(key, file_folder):
if key == keyboard.Key.ctrl_l:
print('ctrl')
with open(file_folder+"/按键日志.txt", "w", encoding='utf-8') as f:
f.write("左Ctrl\n")
else:
with open(file_folder+"/按键日志.txt", "w", encoding='utf-8') as f:
f.write(str(key) + "\n")