Apr-13-2020, 01:02 PM
Hi everyone,
I am working on an app and trying to create a corrector and I am using keyboard module for the correction.
I have created two classes, one keyboard monitor, which reads pressed events and displays them to screen, and one which suppresses them.
What I want to achieve is while the app corrects user input, every text that is typed by the user is suppressed and saved in a variable, for later use.
I am struggling on the synchronization of all this.
What have I missed here?
Thanks in advance.
I am working on an app and trying to create a corrector and I am using keyboard module for the correction.
I have created two classes, one keyboard monitor, which reads pressed events and displays them to screen, and one which suppresses them.
What I want to achieve is while the app corrects user input, every text that is typed by the user is suppressed and saved in a variable, for later use.
I am struggling on the synchronization of all this.
import keyboard import threading import time lock_for_listening_to_keyboard = threading.Lock() global running_suppressed_monitor running_suppressed_monitor = False ######################################################### def delete_and_write(times_to_delete, word_to_write): global running_suppress_monitor print("---Deleting & Rewrite Started---") time.sleep(2) running_suppressed_monitor = False print("---Deleting & Rewrite Ended---") # for i in range(times_to_delete+1): # keyboard.press_and_release('backspace') # for i,char in enumerate(word_to_write): # keyboard.write(char.upper()) # keyboard.write(' ') def write_the_suppressed_string(string): keyboard.write(string) ######################################################### class keyboard_monitor(threading.Thread): def __init__(self,thread_name, threadID, word_typed, keyboard_suppress, counter_for_key_pressed): threading.Thread.__init__(self) self.name = thread_name self.threaID = threadID self.fstring = word_typed self.counter_for_key_presses = counter_for_key_pressed self.suppressed = keyboard_suppress self.temp = "" def stop(self): self._is_running = False def run(self): if (self.suppressed is False): while(True): event = keyboard.read_event(suppress = self.suppressed) if (event.event_type == keyboard.KEY_DOWN): if (event.name == "space"): suppressed_monitor = keyboard_monitor("suppressed_monitor", 2, self.fstring, True, self.counter_for_key_presses) suppressed_monitor.start() suppressed_monitor.join() print("RETURNED TO MAIN MONITOR") self.counter_for_key_presses = 0 self.fstring = "" elif (event.name in "abcdefghijklmnopqrstuvwxyz"): self.fstring = ''.join([self.fstring, event.name]) self.counter_for_key_presses += 1 elif (self.suppressed is True): self.temp = self.fstring self.fstring = "" thread_delete_and_rewrite = threading.Thread( target = delete_and_write, args=(self.counter_for_key_presses, self.temp)) thread_delete_and_rewrite.start() running_suppressed_monitor = True # while(thread_delete_and_rewrite.is_alive()): while(running_suppressed_monitor): event = keyboard.read_event(suppress=self.suppressed) if (event.event_type == keyboard.KEY_DOWN): print("KEYS PRESSED WHILE SUPPRESSED = {}".format(event.name)) if (event.name == "space"): self.temp = self.fstring self.fstring = "" thread_delete_and_rewrite = threading.Thread( target = delete_and_write, args=(self.counter_for_key_presses, self.temp)) thread_delete_and_rewrite.start() # thread_delete_and_rewrite.join() self.counter_for_key_presses = 0 self.fstring = "" elif (event.name in "abcdefghijklmnopqrstuvwxyz"): self.fstring = ''.join([self.fstring, event.name]) self.counter_for_key_presses += 1 # NO thread_delete_and_rewrite.join() # NO thread_delete_and_rewrite.join() if (thread_delete_and_rewrite.is_alive() is False): break thread_delete_and_rewrite.join() print("SELF.FSTRING = {}".format(self.fstring)) print("BEFORE END OF SUPPRESSED MONITOR") if (self.fstring != ""): thread_write = threading.Thread( target = write_the_suppressed_string, args=(self.fstring, )) thread_write.start() thread_write.join() print("SUPPRESSED ENDED") self._is_running = False if __name__ == "__main__": kb_not_suppressed = keyboard_monitor("not_suppressed_monitor", 1, "", False, 0) kb_not_suppressed.start() kb_not_suppressed.join()I have one little problem that when the second while loop ends it requires a key press to exit.
What have I missed here?
Thanks in advance.