检查键盘输入不工作。请帮助

Checking for keyboard inputs not Working. Help Please

我正在尝试此处提供并接受的代码: 但我收到此错误。任何想法为什么?我是新来的,所以如果我违反了这个问题的一些规则,请让我知道,但如果可能的话,请先提出一些解决问题的建议。

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.

或者他们可能更改了模块中的代码。 中的答案是 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)

查看文档中的示例:Example


最终您可以使用 hotkye = read_hotkey(...)if/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()

这就是我解决问题的方法。

我放弃了键盘模块,因为它不能按我想要的方式工作,然后我使用了 Python Global-Hotkeys Module。代码几乎相同,但现在一切都已就位。

希望这对以后的人有所帮助。