调用时未执行 execute() 函数

execute() function is not being executed when called

我正在尝试制作一个 keymap 让我的键盘不使用 Microsoft 无用 IME keyboard。 所以我用 pynput.

写了这个 python 代码

但它似乎没有执行我的execute()功能

   from pynput.keyboard import Key, Controller
   
   current = set()
   keyboard_typ = Controller()
   
   COMBINATIONS = [
   
       {keyboard.KeyCode(char="q")},
       {keyboard.KeyCode(char="Q")},
       {keyboard.KeyCode(char="w")},
       {keyboard.KeyCode(char="W")},
       {keyboard.KeyCode(char="e")},
       {keyboard.KeyCode(char="E")},
       {keyboard.KeyCode(char="r")},
       {keyboard.KeyCode(char="R")},
       {keyboard.KeyCode(char="t")},
       {keyboard.KeyCode(char="T")},
       {keyboard.KeyCode(char="y")},
       {keyboard.KeyCode(char="Y")},
       {keyboard.KeyCode(char="u")},
       {keyboard.KeyCode(char="U")},
       {keyboard.KeyCode(char="i")},
       {keyboard.KeyCode(char="I")},
       {keyboard.KeyCode(char="o")},
       {keyboard.KeyCode(char="O")},
       {keyboard.KeyCode(char="p")},
       {keyboard.KeyCode(char="P")},
       {keyboard.KeyCode(char="a")},
       {keyboard.KeyCode(char="A")},
       {keyboard.KeyCode(char="s")},
       {keyboard.KeyCode(char="S")},
       {keyboard.KeyCode(char="d")},
       {keyboard.KeyCode(char="D")},
       {keyboard.KeyCode(char="f")},
       {keyboard.KeyCode(char="F")},
       {keyboard.KeyCode(char="g")},
       {keyboard.KeyCode(char="G")},
       {keyboard.KeyCode(char="h")},
       {keyboard.KeyCode(char="H")},
       {keyboard.KeyCode(char="j")},
       {keyboard.KeyCode(char="J")},
       {keyboard.KeyCode(char="k")},
       {keyboard.KeyCode(char="K")},
       {keyboard.KeyCode(char="l")},
       {keyboard.KeyCode(char="L")},
       {keyboard.KeyCode(char="z")},
       {keyboard.KeyCode(char="Z")},
       {keyboard.KeyCode(char="x")},
       {keyboard.KeyCode(char="X")},
       {keyboard.KeyCode(char="c")},
       {keyboard.KeyCode(char="C")},
       {keyboard.KeyCode(char="v")},
       {keyboard.KeyCode(char="V")},
       {keyboard.KeyCode(char="b")},
       {keyboard.KeyCode(char="B")},
       {keyboard.KeyCode(char="n")},
       {keyboard.KeyCode(char="N")},
       {keyboard.KeyCode(char="m")},
       {keyboard.KeyCode(char="M")},
   
   ]
   
   def on_press(key) :
   
       if any([key in COMBO for COMBO in COMBINATIONS]):
           current.add(key)
           if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
               execute()
   
   def execute() :
   
       if current == "q":
           
           print("q")
       
   
   def on_release(key) :
   
       if any([key in COMBO for COMBO in COMBINATIONS]):
           current.remove(key)
   
   with keyboard.Listener(on_press=on_press, on_release=on_release) as listener :
       listener.join() 

execute() 有 current 但你从来没有将这样的变量传递给函数,所以你什么都没有比较。您的函数应如下所示

   def execute(current) :
   
       if current == "q":
           
           print("q")

如果您将 current 传递给该函数,它将立即按下所有键并使用循环或 if 条件来检查按下的键中的键。如果您只需要按下最后一个键,则只需传递 key

def on_press(key) :

   if any([key in COMBO for COMBO in COMBINATIONS]):
       current.add(key)
       if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
           execute(current)

def execute(current_key_set) :
   for key in current_key_set:
      print(key)

def on_press(key) :

   if any([key in COMBO for COMBO in COMBINATIONS]):
       current.add(key)
       if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
           execute(key)

def execute(current_key) :
   print(current_key)

在思考 Joaquín 的“设置为字符串”理论后,我将我的 set 转换为 string 它奏效了。

感谢华金。

def execute(current_key) :
    s = str(current_key)
    for i in s :
        if i == "q" :
            print("q")
            #print("\bㅂ") I tested it with a language and it types the normal text too so I prefer adding\b before the letter/word/sentence