Hi,
I am developing an app that monitors and corrects the user input based on some rules.
I am reading the events from keyboard with the keyboard python module.
I faced some problem when the user types very fast, as regards some overlays of text. By this I mean that when my app writes the correct input, the user continues writing and may writes before the corrector types the whole word.
I found, that I can start a keyboard hook with suppressed output to screen and tried to implements a solution.
In the above code I tried recreating the problem and tried giving the general idea.

import keyboard
from collections import deque

string : str = ""
counter : int = 0
is_suppressed: bool = False # this indicates if letters are shown in the window or not
suppressed_string: str = ""
q = deque() # this is used as a buffer, and stores the words that are entered when the
# program is correcting

def keyboard_module_write_to_screen(is_suppressed, string):
        for i in range(len(string) + 1):
            print("--Pressing backspace--")
            keyboard.press_and_release('backspace')
        for i, char in enumerate (string): # simulating a calculation 
            final_char_to_be_written = char.upper()
            print("---WRITING THE CHAR -> {} ---".format(final_char_to_be_written))
            keyboard.write(final_char_to_be_written)
        for i in range(30):
            keyboard.write('*')
        keyboard.write(' ')

def monitoring(event):
        global counter, string, is_suppressed, suppressed_string

        if (event.event_type == keyboard.KEY_DOWN): # and event.name != 'backspace'):
            print("-String entered : {}".format(event.name))

            if (event.name == 'space'):
                # if space is button a new word is entered
                if (is_suppressed is True):
                    # if program occupied writing to the screen save the word to the buffer
                    q.appendleft(suppressed_string)
                    suppressed_string = ""

                elif (is_suppressed is False):
                        # check and write first from the deque,
                        # write the word(s) that were stored in the buffer before writing current
                        # input string
                        # haven't find a way to do the above alongside the others
                        keyboard.unhook_all()
                        keyboard.hook(monitoring, suppress = True)

                        is_suppressed = True

                        keyboard_module_write_to_screen(is_suppressed, string)

                        keyboard.unhook_all()
                        keyboard.hook(monitoring, suppress = False)
                        is_suppressed = False
                        counter = 0
                        string = ""

            elif (event.name in "abcdefghijklmnopqrstuvwxyz") :
                if (is_suppressed is True):
                    suppressed_string = ''.join([suppressed_string, event.name])
                    print("########## SUPPRESSED_STRING = {} #########".format(suppressed_string))

                counter = counter + 1
                print("-- COUNTER is : {}".format(counter))
                string = ''.join([string, event.name])
            elif (event.name == "]"):
                    print(q)
            elif (event.name == 'backspace'):
                pass

keyboard.hook(monitoring, suppress = False)

The main thing I want to achieve is
1)while correcting - writing to the window, read events and save them to a buffer
2)when correcting - writing is done check the buffer, write it's content, but keep reading events
3)if buffer empty and currently not writing something, read events etc.
I didn't manage to make it work and produce the desired result.
Any advice on how to make it work, would be useful.
Thanks in advance for any help.

In my program I have created a Thread that monitors the keyboard and when a condition is True, it corrects the user by deleting and writing the correct word. What I am trying to do as explained above is to suppress the keyboard events when this Thread is correcting.
I searched about it and found that keyboard module has a suppress = True, which doesn't show the keys pressed in the screen. I tried implementing a solution this way, but didn't achieve something.
! Is there any other way of suppressing the event send from keyboard except the keyboard, pynput modules.

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.