[Python] 纯文本查看 复制代码 import PyHook3 as pyhook
import pythoncom
def OnKeyboardDown(event):
global tr1,key_list,ti
if event.Key in key_list:
if tr1 ==0:
tr1=event.Time
return True
elif event.Time-tr1<ti: #100毫秒
print('连击的按键为:',event.Key)
return False
else:
tr1=0
return True
return True
def main():
try:
hm = pyhook.HookManager()
hm.KeyDown = OnKeyboardDown #将OnKeyboardEvent函数绑定到KeyDown事件上
hm.HookKeyboard() #设置键盘钩子
pythoncom.PumpMessages()
except Exception as err:
print(err.__repr__())
input('Error! Any key to Exit. > ')
finally:
hm.UnhookMouse() #取消鼠标钩子
hm.UnhookKeyboard() #取消键盘钩子
if __name__=="__main__":
print('2024-02-03')
tr1=0
try:
t=input('间隔时间:(ms),Enter for 100ms. > ')
ti=int(t)
except:
print('Inter Error. Default is 100ms ')
ti=100 #100毫秒。
print('间隔时间设置为{}毫秒'.format(ti))
print('读取连击按键键值,如需修改,请自行修改本程序下的kbm.ini文件,每行一个键符')
try:
key_list=[]
with open('kbm.ini','r',encoding='utf-8')as fr:
all_lines=fr.readlines()
for each in all_lines:
key_list.append(each.strip())
print('要处理的按键为:',key_list)
if len(key_list)==0:
print('文件读取失败,默认处理R键。')
key_list=['R']
except Exception as err:
print(err.__repr__())
print('文件读取失败,默认处理R键。')
key_list=['R']
print('*'*32,'\n程序运行中……')
main()
|