1 导入模块
import keyboard
2 监听键盘事件
1 添加热键
keyboard.add_hotkey(key,func,args,suppress,timeout,trigger_on_release)
2 删除挂钩热键(必须使用add_hotkey返回的值进行调用)
keybaord.remove_hotkey(hotkey)
3 删除所有键盘热键
keyboard.unhook_all_hotkeys()
4 阻塞,等待键盘事件
keyboard.wait(hotkey=None,suppress=False,trigger_on_release=False)
5 按下事件
keyboard.on_press(func,suppress)
6 释放事件
keyboard.on_release(func,suppress)
7 任意键盘事件
keyboard.hook(func,suppress)
8 指定键盘事件
keyboard.hook_key(hotkey,func,suppress)
9 删除挂钩
keyboard.unhook(remove)
10 删除所有挂钩
keyboard.unhook_all()
11 监听并返回事件对象
recorded=keyboard.read_event()
12 监听并返回键名
key=keyboard.read_key()
13 监听并返回热键
hotkey=keyboard.read_hotkey()
3 模拟键盘事件
1 按下键
keyboard.press(hotkey)
2 释放键
keyboard.release(hotkey)
3 按下并释放键
keyboard.press_and_release(hotkey)
4 添加缩写 键入@@,按空格键入缩写内容
keyboard.abbreviation('@@','email@qq.com')
5 发送事件到系统
keyboard.send(hotkey)
6 键入文本
keyboard.write(text,delay)
4 键属性
1 按下
Keyboard.KEY_DOWN
2 释放
Keyboard.KEY_UP
3 所有修饰符
keyboard.all_modifiers
4 所有双向修饰符
keyboard.sided_modifiers
5 键盘事件属性
1 键名
KeyboardEvent.name
2 事件类型
KeyboardEvent.event_type
3 扫描代码
KeyboardEvent.scan_code
4 修饰符
KeyboardEvent.modifiers
6 解析键盘事件
1 是否按下键
keyboard.is_pressed(hotkey)
2 是否为修饰符
keyboard.is_modifer(key)
3 键名转扫描代码
keyboard.key_to_scan_codes(key)
4 热键转扫描代码
keyboard.parse_hotkey(hotkey)
7 录制事件
1 录制键盘事件
keyboard.start_recording()
2 停止录制
recorded=keyboard.stop_recording()
3 执行录制事件
keyboard.replay(recorded)
4 记录键盘事件
recorded=keyboard.record(until=hotkey)
5 执行键盘事件
keyboard.play(recorded)