python利用 keyboard 库记录键盘事件
今天也不知道是想了什么,突然就想要试试看我有效击键时的手速到底有多快。为此,需要记录下来击键的记录。于是找到了Python的keyboard库。
安装非常简单,只需执行pipinstallkeyboard即可。
键盘事件录制
保存并执行如下代码
importkeyboard importtime keyboard.hook(lambdae:print(e,time.clock())) keyboard.wait('Ctrl')
这里,我们利用keyboard.hook为每个键盘事件下钩子,并且执行其中的lambda函数。keyboard.wait()的作用是阻塞进程,直到按下Ctrl键。
测试结果如下:
KeyboardEvent(gdown)4.450576466 KeyboardEvent(gup)4.505627652 KeyboardEvent(idown)4.510628277 KeyboardEvent(tdown)4.570555791 KeyboardEvent(iup)4.580582066 KeyboardEvent(spacedown)4.581583136 KeyboardEvent(pdown)4.630656009 KeyboardEvent(tup)4.671189791 KeyboardEvent(spaceup)4.693182730 KeyboardEvent(pup)4.705543556 KeyboardEvent(udown)4.705867633 KeyboardEvent(sdown)4.730623806 KeyboardEvent(uup)4.750832241 KeyboardEvent(sup)4.795770594 KeyboardEvent(hdown)4.810543976 KeyboardEvent(hup)4.86056459 KeyboardEvent(enterdown)4.920739469 KeyboardEvent(enterup)4.930514276 KeyboardEvent(ctrldown)7.030813124
从中可见,在敲下gitpush
热键捕获绑定
importkeyboard print('Pressandreleaseyourdesiredhotkey:') hotkey=keyboard.read_hotkey() print('Hotkeyselected:',hotkey) defon_triggered(): print("Triggered!") keyboard.add_hotkey(hotkey,on_triggered) print("PressESCtostop.")
以上就是python利用keyboard库记录键盘事件的详细内容,更多关于python记录键盘事件的资料请关注毛票票其它相关文章!