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 a class, which reads pressed events and displays them to screen, and its action are dependent to a variable passed to the constructor.
If the user input then the suppress part is executed, but when not the other part is executed.
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 have managed to make it work in paralell, but having some problems.
Please see the code below.

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 am trying to return the control when the function of correction is done, but can't find the correct way
(what I want is the second while loop to end when the correct and rewrite is done, now it ends when the correct and rewrite is done and a button is pressed ).
Any help is appreciated.

Aww, I really wish I could help you, but unfortunately I don't know Python. Perhaps the least I can do is bump this thread and hopefully it can get some renewed interest. That is, unless you have already figured out what was wrong.

You're referring to the while loop that begins on line 74, correct?

I'm completely grasping at straws here, but what about a do while loop instead of a while loop?

commented: Hi Dani, thanks for answering, after a lot of search I managed to solve it with threading events of Python. Thanks for the interest. +0

Sorry I couldn’t help more. I’ve marked the topic as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.