python - 检查键盘输入不起作用。请帮忙
问题描述
我正在尝试在此处提供并接受此代码:https ://stackoverflow.com/a/55369170/14307622 但我收到此错误。任何想法为什么?我是新来的,所以如果我违反了这个问题的一些规则,那么请告诉我,但如果可能的话,请先提出一些解决问题的方法。
import keyboard
import time
listedSongs = []
currentSong = "idk"
exit = False # make a loop control variable
def alt_k():
i = 1
paused = False
def alt_q():
global exit
exit = True
def alt_s():
if currentSong not in listedSongs:
listedSongs.append(currentSong)
print(listedSongs)
# assign hooks to the keyboard
keyboard.on_press_key("alt+k", alt_k) # on press alt+k, execute alt_k()
keyboard.on_press_key("alt+q", alt_q)
keyboard.on_press_key("alt+s", alt_s)
# main loop
while not exit:
keyboard.wait() # "block" for input (essentially, do nothing until a key is pressed and yield CPU resources to anything else that wants them)
我得到的错误是:
Traceback (most recent call last):
File "D:\AJ\Coding\test.py", line 26, in <module>
keyboard.on_press_key("alt+k", alt_k) # on press alt+k, execute alt_k()
File "C:\Users\AJ\AppData\Local\Programs\Python\Python39\lib\site-packages\keyboard\__init__.py", line 510, in on_press_key
return hook_key(key, lambda e: e.event_type == KEY_UP or callback(e), suppress=suppress)
File "C:\Users\AJ\AppData\Local\Programs\Python\Python39\lib\site-packages\keyboard\__init__.py", line 493, in hook_key
scan_codes = key_to_scan_codes(key)
File "C:\Users\AJ\AppData\Local\Programs\Python\Python39\lib\site-packages\keyboard\__init__.py", line 324, in key_to_scan_codes
raise ValueError('Key {} is not mapped to any known key.'.format(repr(key)), e)
ValueError: ("Key 'alt+k' is not mapped to any known key.", ValueError("Key name 'alt+k' is not mapped to any known key."))
解决方案
它似乎on_press_key()
只适用于单键,q
但不适用于组合alt+q
。或者,这可能只是某些系统上的问题。至少它在我的 Linux 上不起作用。
或者他们可能会更改模块中的代码。检查键盘输入中的答案使用过多的 cpu 使用率,我的代码有问题吗?2岁。
你可以使用add_hotkey()
它不需要wait()
import keyboard
import time
listedSongs = []
currentSong = "idk"
exit = False # make a loop control variable
def alt_k():
print('pressed: alt+k')
i = 1
paused = False
def alt_q():
global exit # need it to assign `True` to global/external variable instead of creating local variable
print('pressed: alt+q')
exit = True
def alt_s():
print('pressed: alt+s')
if currentSong not in listedSongs:
listedSongs.append(currentSong)
print(listedSongs)
keyboard.add_hotkey('alt+k', alt_k)
keyboard.add_hotkey('alt+q', alt_q)
keyboard.add_hotkey('alt+s', alt_s)
# main loop
while not exit:
time.sleep(1)
请参阅文档中的示例:示例
最终您可以使用hotkye = read_hotkey(...)
withif/else
来执行正确的功能。
我不确定,但有时它也适用于我,hotkey = keyboard.wait(suppress=False)
但有时它不起作用。
while not exit:
hotkey = keyboard.read_hotkey(suppress=False)
#hotkey = keyboard.wait(suppress=False)
print('hotkey:', hotkey)
if hotkey == 'alt+k':
alt_k()
elif hotkey == 'alt+q':
alt_q()
推荐阅读
- xamarin.forms - 使指南针始终指向 Xamarin 表单中的特定位置
- terminal - Visual Studio 代码:以退出代码终止:3221225477
- r - 如何将一列数据附加到R中的多列
- android - 如何在意图android中传递2个文件
- php - 如何在 Twig 中的元素上添加多个 ID
- r - R中的ifelse命令没有填充数据框中的列?
- reactjs - 尝试通过道具向子级提升功能时超出最大更新深度
- sql-server - SQL Server 如何使用“。”映射我的服务器。作为服务器名称?
- javascript - 与多个对象数组相交
- r - 如果不是所有列都存在于 R 中的所有数据框中,则按列合并数据框