在windows和linux上钩住并模拟键盘事件
keyboard的Python项目详细描述
使用这个小python库完全控制键盘。钩子 全局事件、注册热键、模拟按键等等。
功能
- global event hook在所有键盘上(无论 聚焦)。
- listen和send键盘事件。
- 使用windows和linux(需要sudo),使用 实验性的os x支持(感谢@glitchassasin!).
- pure python,没有要编译的c模块。
- 0依赖项。安装和部署很简单,只需复制 文件夹。
- python 2和3。
- 复杂热键支持(例如ctrl+shift+m, ctrl+space),具有 可控超时。
- 包括高级api(例如record和 play, add_abbreviation)。
- 将键映射为它们在布局中的实际位置,full 国际化支持(例如Ctrl+ç)。
- 在单独的线程中自动捕获的事件,不阻止main 程序。
- 测试和记录。
- 不会弄坏有口音的死键(我在看着你,派胡克)。
- 通过项目提供鼠标支持 mouse(pip install mouse)。
用法
安装PyPI package:
pip install keyboard
或者克隆存储库(不需要安装,源文件是 足够):
git clone https://github.com/boppreh/keyboard
或download and extract the zip进入 你的项目文件夹。
然后检查API docs below查看哪些特性 有空。
示例
importkeyboardkeyboard.press_and_release('shift+s, space')keyboard.write('The quick brown fox jumps over the lazy dog.')keyboard.add_hotkey('ctrl+shift+a',print,args=('triggered','hotkey'))# Press PAGE UP then PAGE DOWN to type "foobar".keyboard.add_hotkey('page up, page down',lambda:keyboard.write('foobar'))# Blocks until you press esc.keyboard.wait('esc')# Record events until 'esc' is pressed.recorded=keyboard.record(until='esc')# Then replay back at three times the speed.keyboard.play(recorded,speed_factor=3)# Type @@ then press space to replace with abbreviation.keyboard.add_abbreviation('@@','my.long.email@example.com')# Block forever, like `while True`.keyboard.wait()