Python-键盘模块-线程问题

时间:2020-04-12 18:24:54

标签: python multithreading keyboard python-multithreading


大家好,
我正在开发一个应用,并尝试创建一个校正器,并且正在使用键盘模块进行校正。
我创建了两个类,一个类是键盘监视器,它可以读取按下的事件并将其显示在屏幕上,而另一个类则是隐藏它们。
我想要实现的是,当应用程序纠正用户输入时,用户输入的每个文本都会被隐藏并保存在变量中,以供以后使用。
我正在努力实现所有这些同步。

import keyboard
import threading
import time

lock_for_listening_to_keyboard = threading.Lock()

#########################################################
def delete_and_write(times_to_delete, word_to_write):
    global lock_for_listening_to_keyboard

    time.sleep(2)
    print("OK")
    # for i in range(50):
    #     keyboard.write('*')
    # 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())

    # for i in range(40):
    #     keyboard.write('*')

    # keyboard.write(' ')


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


class keyboard_not_suppressed_monitor(threading.Thread):

    def __init__(self, threadID,  keyboardSupress):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.fstring = ""
        self.counter_for_key_presses = 0
        self.suppressed = False

    def run(self):

        while(True):
            event = keyboard.read_event(suppress=self.suppressed)

            if (event.event_type == keyboard.KEY_DOWN):
                # print("Key pressed = {} + suppress = {}".format(event.name, self.suppressed))

                if (event.name == "space"):

                    suppressed_monitoring = keyboard_suppressed_monitor(2, self.fstring, self.counter_for_key_presses, None)
                    suppressed_monitoring.start()
                    suppressed_monitoring.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



class keyboard_suppressed_monitor(threading.Thread):

    def __init__(self, threadID, fstring, counter_for_key_presses, keyboardSupress):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.fstring = fstring
        self.counter_for_key_presses = counter_for_key_presses
        self.suppressed = True
        self.done = False
        self.temp = ""

    def stop(self):
        self._is_running = False


    def run(self):

        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() # join is not here
        print("RETURNED FROM THE REWRITER")

        while(True):
            print("STATE OF THREAD IS : {}".format(thread_delete_and_rewrite.is_alive()))          print("IN THE WHILE TRUE")

            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"):
                    print("THE STRING ENTERED ")
                    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()

                    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
        # thread_delete_and_rewrite.join() # join is not here
        print("SUPPRESSED ENDED")
        self._is_running = False


if __name__ == "__main__":
    kb = keyboard_not_suppressed_monitor(1, None)
    kb.start()
    kb.join()

我正在尝试在完成校正功能后返回控件,但无法使其正常工作。
任何帮助表示赞赏。

*更新1 * 我上了一堂课,作品几乎完美。
我有一个小问题,当第二个while循环结束时,需要按一下退出键。有解决方案吗?

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

0 个答案:

没有答案