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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
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() |
What have I missed here?
Thanks in advance.