我编写了下面的击键系统,该系统将on_press()函数和on_release()?函数的值返回到名为key_dict. 运行脚本,即使我已经在两个函数中全局定义了它,也会返回一个 NameError。
有任何想法吗?谢谢
from pynput.keyboard import Key, Listener
from datetime import datetime
def on_press(key):
global key_p,key_pt,key_r,key_rt
key_p = key
key_pt = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')
keyall = {'Key_Pressed':key_p, 'Key_Time':key_pt}
return key_p, key_pt
def on_release(key):
global key_p,key_pt,key_r,key_rt
key_r = key
key_rt = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')
keysall = {'Key_Released':key_r, 'Release_Time':key_rt}
return key_r, key_rt
if key == Key.enter:
# Stop listener
return False
key_dict = {'Key_Pressed':[key_p],
'Key_Time':[key_pt],
'Key_Released':[key_r],
'Release_Time':[key_rt]}
print(key_dict)
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
阿晨1998
相关分类