Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am using the pynput module and I want to restart a listener after a specific rule - condition - key combination is fulfilled.
The code I have written is shown below :
from pynput.keyboard import Key, Listener
from langdetect import detect
from pynput import keyboard
def listener_manager():
#the idea is to return a new listener when needed
listener = keyboard.Listener(on_press=on_press)
return listener
def on_press(key):
global string
global listener
global controller
if key == keyboard.Key.esc: #if button escape is pressed close the program
listener.stop()
elif key == keyboard.Key.space:
print(string)
string=""
elif key == keyboard.Key.shift:
listener.stop()
controller = keyboard.Controller()
listener = listener_manager()
listener.start()
print("New listener started")
elif key== keyboard.Key.alt_l:
listener.stop()
controller = keyboard.Controller()
listener = listener_manager()
listener.start()
print("Left alt pressed")
else:
string = ''.join([string,str(key).replace("'","")])
string=""
"""This is the beginning"""
controller = keyboard.Controller()
# Collect events until released
listener = keyboard.Listener(on_press=on_press)
listener.start()
Basically, I want to restart the listener when shift or alt (OR when language is changed), but the code I wrote didn't really work.
I've found in my own program that this works:
listener = keyboard.Listener(on_press=on_press)
Without using join
works with the above code.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.