0
即時編程相當新穎,我希望我可以準確地描述我的問題。 我想監視輸入我的鍵盤,但我得到這個TypeError:on_press()缺少1所需的位置參數:'鑰匙'。TypeError:on_press()缺少1個必需的位置參數:'key'
當我單獨實現該功能時,它工作正常。
from pynput import keyboard
def on_press(key):
keys=[]
try: k = key.char # single-char keys
except: k = key.name # other keys
#if key == keyboard.Key.esc: return False # stop listener
if k in ['down', 'left', 'right',"up"]: # keys interested
# self.keys.append(k) # store it in global-like variable
#print('Key pressed: ' + k)
keys.append(k)
return keys
lis = keyboard.Listener(on_press=on_press)
lis.start() # start to listen on a separate thread
lis.join() # no this if main thread is polling self.keys
...但是,如果我從主
pressed_key = on_press()
調用該函數出現以下錯誤:
File "<ipython-input-7-c3f93c434982>", line 80, in main
pressed_key = on_press()
TypeError: on_press() missing 1 required positional argument: 'key'
請讓我知道如果你有任何想法!
錯誤消息告訴你缺少一個參數。 'on_press()'函數採用被按下的鍵,而你沒有通過該鍵。 – kindall
請正確縮進您的代碼 – jacoblaw