ppel123 32 Newbie Poster

Hello,
I have created a spelling corrector for a project and currently I am struggling on making it work in real-time application (on-the-fly)
By this I mean, that I am reading the input of the user (anywhere on the Windows OS - not only in python shell) with the python keyboard library and when the user enters a space I am checking the spelling of the entered word.
If it is correct, the program does nothing but if it is wrong the program deletes the wrong word and re-writes the correct one.
I have implemented the last functionality of deletion-and-rewrite with the pynput (press and release), but the performance is nowhere near good.
This is the last serious functionality I should implement, so any help would be crusial.
An example of the code that I am using is shown below.

import keyboard
from pynput.keyboard import Key,Controller

pynput_keyboard = Controller()
word_list = ['one', 'two', 'three'] # we suppose these are the correct words
correct_word = "CORRECT"

string = ""
while True:
    event = keyboard.read_event()
    if (event.event_type == keyboard.KEY_DOWN):
        key_pressed = event.name
        print("Key pressed is {}".format(key_pressed))
        print("String is {}".format(string))
        if (key_pressed == "space"):
            if (string in word_list):
                string = ""
                pass
            else :
                for i in range(len(string) + 1):
                    # print("IN DELETION")
                    pynput_keyboard.press(Key.backspace)
                    pynput_keyboard.release(Key.backspace)

                for i, char in enumerate(correct_word):
                    # print("in rewrite")
                    pynput_keyboard.press(char)
                    pynput_keyboard.release(char)
                pynput_keyboard.press(' ')
                pynput_keyboard.release(' ')
                string=""

        elif (key_pressed in "abcdefghijklmnopqrstuvwxyz"):

            string = ''.join([string, str(key_pressed)])

I added a sample code for better understanding and a screenshot indicating an error that occurs if a user types very fast.
The main problem here is that the program delays on deleting and writing the correct word and the user overlays the words written from the program.
Are there any effective ways of doing the above and correcting the problem that occurs?
Thanks in advance.

img.png

Dani commented: Sorry I can't be of assistance but unfortunately I don't know Python +32