跳转到主要内容

CircuitPython的HID设备模拟辅助库。

项目描述

简介

Documentation Status Discord Build Status Code Style: Black

此驱动程序模拟USB HID设备。目前实现了键盘和鼠标。

依赖关系

此驱动程序依赖于

请确保所有依赖项在CircuitPython文件系统中可用。这可以通过下载Adafruit库和驱动程序捆绑包轻松实现。

附加布局

此库有一个en-US布局。请查看并扩展Neradoc的库以获取更多布局。

使用示例

Keyboard类将USB键盘设备的按键报告发送到主机。

Keycode类定义了使用Keyboard发送的USB HID键码。

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# Set up a keyboard device.
kbd = Keyboard(usb_hid.devices)

# Type lowercase 'a'. Presses the 'a' key and releases it.
kbd.send(Keycode.A)

# Type capital 'A'.
kbd.send(Keycode.SHIFT, Keycode.A)

# Type control-x.
kbd.send(Keycode.CONTROL, Keycode.X)

# You can also control press and release actions separately.
kbd.press(Keycode.CONTROL, Keycode.X)
kbd.release_all()

# Press and hold the shifted '1' key to get '!' (exclamation mark).
kbd.press(Keycode.SHIFT, Keycode.ONE)
# Release the ONE key and send another report.
kbd.release(Keycode.ONE)
# Press shifted '2' to get '@'.
kbd.press(Keycode.TWO)
# Release all keys.
kbd.release_all()

KeyboardLayoutUS通过按键发送ASCII字符。它假定主机设置为接受来自US键盘的按键。

如果主机期望使用非美国键盘,由 KeyboardLayoutUS 提供的字符到键的映射可能并不总是正确的。在某些情况下需要不同的按键。例如,要在法语键盘(AZERTY而不是QWERTY)上输入'A',应该按下 Keycode.Q

目前此包只提供 KeyboardLayoutUS。可以添加更多 KeyboardLayout 类来处理非美国键盘以及各种操作系统提供的不同输入方法。

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)

# Type 'abc' followed by Enter (a newline).
layout.write('abc\n')

# Get the keycodes needed to type a '$'.
# The method will return (Keycode.SHIFT, Keycode.FOUR).
keycodes = layout.keycodes('$')

Mouse 类模拟带有滚轮的三键鼠标。

import usb_hid
from adafruit_hid.mouse import Mouse

m = Mouse(usb_hid.devices)

# Click the left mouse button.
m.click(Mouse.LEFT_BUTTON)

# Move the mouse diagonally to the upper left.
m.move(-100, -100, 0)

# Roll the mouse wheel away from the user one unit.
# Amount scrolled depends on the host.
m.move(0, 0, -1)

# Keyword arguments may also be used. Omitted arguments default to 0.
m.move(x=-100, y=-100)
m.move(wheel=-1)

# Move the mouse while holding down the left button. (click-drag).
m.press(Mouse.LEFT_BUTTON)
m.move(x=50, y=20)
m.release_all()       # or m.release(Mouse.LEFT_BUTTON)

ConsumerControl 类模拟消费类控制设备,例如遥控器或某些键盘上的多媒体键。

import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

cc = ConsumerControl(usb_hid.devices)

# Raise volume.
cc.send(ConsumerControlCode.VOLUME_INCREMENT)

# Pause or resume playback.
cc.send(ConsumerControlCode.PLAY_PAUSE)

文档

该库的API文档可在 Read the Docs 上找到。

有关构建库文档的信息,请参阅 本指南

贡献

欢迎贡献!在为此项目做出贡献之前,请阅读我们的 行为准则,以帮助保持项目的包容性。

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。

源分布

adafruit_circuitpython_hid-6.1.2.tar.gz (40.0 kB 查看哈希值)

上传时间 源代码

构建分布

adafruit_circuitpython_hid-6.1.2-py3-none-any.whl (18.6 kB 查看哈希值)

上传时间 Python 3

支持者