boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.76k stars 433 forks source link

Cant detect keypress #556

Open ghost opened 2 years ago

ghost commented 2 years ago

image

I am inside a class, lets call it TempClass, it has self.scrape1 to self.scrape9 methodd which is to be added to respective hotkeys Then I have temp function of TempClass called self.temp(x). From main function, its for looping and calling t = TempClass(), t.temp(var) where var is for looping variable. So, problem in temp() function, start_listener doesnt work on second loop, The start_listener() function works properly on first loop. It runs the function self.scrape8 or self.scrape9. But when I do Ctrl+C, then t.temp(x) gets ran again from for loop, it prints Listening. But it gets stuck and doesn't detect keypress from onward second for loop and doesnt even run self.scrape8 or self.scrape9 on pressing 8 or 9

ghost commented 2 years ago
class E:
    def temp(self, x):
        print(x)
        def start_listener():
            import keyboard
            def listener():
                keyboard.read_key()
            keyboard.add_hotkey('1', self.print1)
            keyboard.add_hotkey('2', self.print1)
            while True:
                try:
                    print("L")
                    listener()
                except KeyboardInterrupt:
                    del keyboard
                    break
        start_listener()
        input(":> ")

    def print1(self):
        print(1)

    def print2(self):
        print(2)

e = E()
for _ in range(100):
    from time import sleep
    sleep(1)
    e.temp('x')
ghost commented 2 years ago

Try it yourself on linux, 1 and 2 shouldnt work when _ = 2

ghost commented 2 years ago

python3 -m pip show keyboard Name: keyboard Version: 0.13.5 Summary: Hook and simulate keyboard events on Windows and Linux Home-page: https://github.com/boppreh/keyboard Author: BoppreH Author-email: boppreh@gmail.com License: MIT Location: /usr/local/lib/python3.10/dist-packages Requires: Required-by: