我在带有键盘库的python中使用键侦听器。但是我得到一个错误。我不明白为什么。此功能概括如下:
我随机选择一个数字。然后,我从oruntu列表中随机选择一个数字。
像这样处理:
随机数:40
从oruntu中随机选择数字:-1
40 + =(-1)
39 + =(-1)
38 + =(-1)
然后,如果我按了正确的键,则意味着“ f”键,再次启动function2。然后再次重复,直到我按下“ esc”键。
def function2():
keys = {"a": -4, "s":-3, "d":-2 , "f":-1, "esc":0}
oruntu = [-4,-3,-2,-1]
sayi = random.randrange(0,99)
randChoice = choice(oruntu)
toplam = sayi
while(True):
b = randChoice
toplam += randChoice
print(toplam)
def listen(key):
while True:
keyboard.wait(key)
if(keys[key]== randChoice):
function2()
if(keys[key]== 0):
break
threadss = [Thread(target=listen, kwargs={"key":key}) for key in keys]
for thread in threadss:
thread.start()
sleep(0.5)
t = threading.Thread(target=function2)
t.start()
错误:
keyboard\__init__.py", line 653, in remove_
del _hotkeys[hotkey]
KeyError: 's'
Exception in thread Thread-10:
Traceback (most recent call last):
File "C:\Users\ozgey\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\ozgey\AppData\Local\Programs\Python\Python37 \lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\ozgey\Desktop\deneme.py", line 20, in listen
keyboard.wait(key)
File "C:\Users\ozgey\AppData\Local\Programs\Python\Python37\lib\site-packages\keyboard\__init__.py", line 883, in wait
remove_hotkey(remove)
File "C:\Users\ozgey\AppData\Local\Programs\Python\Python37\lib\site-packages\keyboard\__init__.py", line 750, in remove_hotkey
_hotkeys[hotkey_or_callback]()
File "C:\Users\ozgey\AppData\Local\Programs\Python\Python37\lib\site-packages\keyboard\__init__.py", line 653, in remove_
del _hotkeys[hotkey]
KeyError: 's'