boppreh / keyboard

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

Calling stop_recording() twice triggers a KeyError #547

Open jmmelko opened 2 years ago

jmmelko commented 2 years ago
import keyboard

keyboard.start_recording()
Out[13]: (<queue.Queue at 0x222d1c98438>, <function keyboard.hook.<locals>.remove_()>)

keyboard.stop_recording()
Out[14]: 
[KeyboardEvent(haut down),
 KeyboardEvent(haut up),
 KeyboardEvent(haut down),
 KeyboardEvent(haut up),
 KeyboardEvent(haut down),
 KeyboardEvent(haut up),
 KeyboardEvent(haut down),
 KeyboardEvent(haut up),
 KeyboardEvent(enter down),
 KeyboardEvent(enter up)]

keyboard.stop_recording()
Traceback (most recent call last):

  File "<ipython-input-15-2fea8506ff73>", line 1, in <module>
    keyboard.stop_recording()

  File "D:\jmmelkon\WinPython\python-3.7.2.amd64\lib\site-packages\keyboard\__init__.py", line 1028, in stop_recording
    unhook(hooked)

  File "D:\jmmelkon\WinPython\python-3.7.2.amd64\lib\site-packages\keyboard\__init__.py", line 523, in unhook
    _hooks[remove]()

KeyError: <function hook.<locals>.remove_ at 0x00000222D1F200D0>

In[15]: keyboard._recording
Ou[15]: (<queue.Queue at 0x222d1c98438>, <function keyboard.hook.<locals>.remove_()>)

The function should either pass, or return the same error as when start_recording as not be called first

Worse, the state of _recording should have been set to None