Python Forum
Keyboard Module Python - Suppress input while writing to window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyboard Module Python - Suppress input while writing to window
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,745 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 1,307 Sep-25-2023, 08:07 PM
Last Post: deanhystad
  Help Switching between keyboard/Mic input in my code Extra 1 1,084 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,167 Jun-13-2022, 08:59 AM
Last Post: Shena76
  keyboard module; must be root problem philipbergwerf 2 19,037 Apr-04-2021, 11:40 AM
Last Post: philipbergwerf
  keyboard module question DPaul 0 2,131 Mar-23-2021, 04:22 PM
Last Post: DPaul
  how to suppress not to display the package contents from pydoc! maiya 3 2,470 Mar-19-2021, 03:41 AM
Last Post: bowlofred
  keyboard module doesn't work in the microsoft version terminal of python. username 1 2,802 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  Python - Keyboard module - Threading problem ppel123 1 3,061 Apr-13-2020, 04:49 PM
Last Post: deanhystad
  Python - Most effective way to correct keyboard-user-input. ppel123 8 4,152 Apr-08-2020, 07:41 AM
Last Post: ppel123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020