1 导入模块


  1. import keyboard

2 监听键盘事件


1 添加热键

  1. keyboard.add_hotkey(key,func,args,suppress,timeout,trigger_on_release)

2 删除挂钩热键(必须使用add_hotkey返回的值进行调用)

  1. keybaord.remove_hotkey(hotkey)

3 删除所有键盘热键

  1. keyboard.unhook_all_hotkeys()

4 阻塞,等待键盘事件

  1. keyboard.wait(hotkey=None,suppress=False,trigger_on_release=False)

5 按下事件

  1. keyboard.on_press(func,suppress)

6 释放事件

  1. keyboard.on_release(func,suppress)

7 任意键盘事件

  1. keyboard.hook(func,suppress)

8 指定键盘事件

  1. keyboard.hook_key(hotkey,func,suppress)

9 删除挂钩

  1. keyboard.unhook(remove)

10 删除所有挂钩

  1. keyboard.unhook_all()

11 监听并返回事件对象

  1. recorded=keyboard.read_event()

12 监听并返回键名

  1. key=keyboard.read_key()

13 监听并返回热键

  1. hotkey=keyboard.read_hotkey()

3 模拟键盘事件


1 按下键

  1. keyboard.press(hotkey)

2 释放键

  1. keyboard.release(hotkey)

3 按下并释放键

  1. keyboard.press_and_release(hotkey)

4 添加缩写 键入@@,按空格键入缩写内容

  1. keyboard.abbreviation('@@','email@qq.com')

5 发送事件到系统

  1. keyboard.send(hotkey)

6 键入文本

  1. keyboard.write(text,delay)

4 键属性


1 按下

  1. Keyboard.KEY_DOWN

2 释放

  1. Keyboard.KEY_UP

3 所有修饰符

  1. keyboard.all_modifiers

4 所有双向修饰符

  1. keyboard.sided_modifiers

5 键盘事件属性


1 键名

  1. KeyboardEvent.name

2 事件类型

  1. KeyboardEvent.event_type

3 扫描代码

  1. KeyboardEvent.scan_code

4 修饰符

  1. KeyboardEvent.modifiers

6 解析键盘事件


1 是否按下键

  1. keyboard.is_pressed(hotkey)

2 是否为修饰符

  1. keyboard.is_modifer(key)

3 键名转扫描代码

  1. keyboard.key_to_scan_codes(key)

4 热键转扫描代码

  1. keyboard.parse_hotkey(hotkey)

7 录制事件


1 录制键盘事件

  1. keyboard.start_recording()

2 停止录制

  1. recorded=keyboard.stop_recording()

3 执行录制事件

  1. keyboard.replay(recorded)

4 记录键盘事件

  1. recorded=keyboard.record(until=hotkey)

5 执行键盘事件

  1. keyboard.play(recorded)