【发布时间】:2021-11-06 20:44:02
【问题描述】:
我是 python 新手,我正在尝试模仿键盘记录器。我想当我按退格键时,它会删除文本文件中的最后一个字符,比如键入“helli”,字母“i”是错误的,我按退格键,“i”字母被删除。有人有想法吗?
from pynput.keyboard import Listener
def anonymous(key):
key = str(key)
key = key.replace("'","")
if key == "Key.esc":
raise SystemExit(0)
if key == "Key.ctrl_l":
key = "ctrl"
if key == "Key.enter":
key = "\n"
if key == "Key.caps_lock":
key = "capslock"
if key == "Key.tab":
key = "\n"
if key == "Key.ctrl_r":
key = "ctrl"
if key == "Key.space":
key = " "
if key == "Key.backspace"
#this line
with open("log.txt", "a") as file:
file.write(key)
print(key)
with Listener(on_press=anonymous) as listener:
listener.join()
【问题讨论】:
-
这能回答你的问题吗? Remove very last character in file
-
执行 key=key[:-1] 并在最后保存文件。
-
如果我使用 key=key[:-1] 将是“backspac”
标签: python-3.x character keylogger