,

Simulating USB HID Devices with Adafruit CircuitPython HID

Lake Davenberg Avatar

·

USB HID (Human Interface Device) devices, such as keyboards, mice, and consumer control devices, play a crucial role in modern human-computer interaction. In this article, we will explore how to simulate and control these HID devices using Adafruit CircuitPython HID library.

Example Code Implementations

  1. Simulating Keyboard Inputs
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()
  1. Simulating Mouse Inputs
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)
  1. Emulating Consumer Control Devices
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)

Discussion

The Adafruit CircuitPython HID library provides a convenient way to simulate USB HID devices using CircuitPython. In the first example, we see how to create keyboard inputs by using the Keyboard class. By sending keycodes defined in the Keycode class, we can simulate keypresses, modifiers, and special characters. This makes it easy to automate keyboard actions or create custom input devices.

The second example demonstrates how to create mouse inputs using the Mouse class. By providing movement and button click commands, we can simulate mouse movements, button presses, and even scroll wheel actions. This allows us to control the mouse cursor remotely or automate mouse actions in repetitive tasks.

Lastly, the Consumer Control example shows how to emulate consumer control devices like remote controls or multimedia keys. By using the ConsumerControl class and the predefined ConsumerControlCode constants, we can send commands to control various media playback functions such as volume control, play/pause, and more.

By combining these examples and exploring the available classes and methods in the Adafruit CircuitPython HID library, you can create your own custom HID devices. Whether it’s a custom keyboard for gaming, a gesture-based mouse, or a remote control for your media center, the possibilities are endless.

Categorization

  • Technology: IoT, Embedded Systems
  • Tools: CircuitPython, Adafruit CircuitPython HID library, USB HID devices
  • Packages: usb_hid, adafruit_hid

Tags

  • Python
  • CircuitPython
  • HID
  • USB
  • Keyboard
  • Mouse
  • IoT
  • Embedded Systems

In conclusion, the Adafruit CircuitPython HID library provides a convenient and flexible way to simulate USB HID devices using CircuitPython. This article has provided example code implementations for creating keyboard and mouse inputs, as well as emulating consumer control devices. By exploring the possibilities of creating your own HID devices with CircuitPython, you can unlock new possibilities in human-computer interaction and automation. Start experimenting and create your own custom HID devices today!

Leave a Reply

Your email address will not be published. Required fields are marked *