Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python键盘模块-退出阻止读取事件函数_Python_Python 3.x_Keyboard_Python Multithreading - Fatal编程技术网

Python键盘模块-退出阻止读取事件函数

Python键盘模块-退出阻止读取事件函数,python,python-3.x,keyboard,python-multithreading,Python,Python 3.x,Keyboard,Python Multithreading,你好,, 下面是更正用户输入的代码,当控件从更正线程返回时,我想退出阻塞函数keyboard.read_事件。 整个程序运行良好,但我无法在更正线程完成后立即退出,程序等待按键。 我尝试使用自定义异常来中断keyboard.read_事件函数,但没有成功 任何关于如何退出这个阻塞函数的想法都是非常有用的。 提前感谢。除非您发现某个keyboard.read\u事件有超时,或者如果有事件,则执行非阻塞检查,否则这是不可能的。我没有在键盘模块中找到任何一个/ 一个大的解决方法是使用键盘。按下以防退出

你好,, 下面是更正用户输入的代码,当控件从更正线程返回时,我想退出阻塞函数keyboard.read_事件。 整个程序运行良好,但我无法在更正线程完成后立即退出,程序等待按键。 我尝试使用自定义异常来中断keyboard.read_事件函数,但没有成功

任何关于如何退出这个阻塞函数的想法都是非常有用的。
提前感谢。

除非您发现某个keyboard.read\u事件有超时,或者如果有事件,则执行非阻塞检查,否则这是不可能的。我没有在键盘模块中找到任何一个/

一个大的解决方法是使用键盘。按下以防退出。不确定是否可以检测到它是否来自用户。这取决于你是否可以接受

import keyboard
import threading
import time

class Interrupt_Custom_Exception(Exception):
   """Base class for other exceptions"""
   pass

#########################################################
def delete_and_write(times_to_delete, word_to_write):
    print("------------Deleting & Rewrite Started---")
    time.sleep(2)
    print("------------Deleting & Rewrite Ended---")
    # simulate deletion and rewrite
    #**here I tried the raise Interrupt_Custom_Exception and tried to catch it at the code in the class, but didn't work**


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):

            def listen_to_keyboard():
                event = keyboard.read_event(suppress=self.suppressed)
                # **here is where the program waits and don't continue when the correction thread is  finished.**
                if (event.event_type == keyboard.KEY_DOWN):
                    print("---KEYS PRESSED WHILE SUPPRESSED = {}---".format(event.name))
                    if (event.name in "abcdefghijklmnopqrstuvwxyz"):
                        self.fstring = ''.join([self.fstring, event.name])
                        self.counter_for_key_presses += 1

            try:

                #########################################################
                # INITIALY CORRECTING THE WORD PASSED FROM THE NORMAL KEY MONITOR
                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()
                # raise Interrupt_Custom_Exception

                #########################################################

                print("-BEFORE WHILE LOOP-")
                while(thread_delete_and_rewrite.is_alive() is True): # **this works ok but if the control enters the listen_to_keyboard function waits there until a key is pressed. I want somehow to stop this manually and continue the code after this while**
                    print("--ENTERING THE WHILE LOOP--")
                    listen_to_keyboard()
                    print("----EXITING THE WHILE LOOP----\n")            
            except Interrupt_Custom_Exception:
                print("!!!!!!!!!!!!!!!!!CAUGHT IT!!!!!!!!!!!!!!!!!!!")
                print("----EXITING THE WHILE LOOP----\n")

            print("------BEFORE FINAL WRITE------")

            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()