Introduction

Documentation Status Discord Build Status

This driver simulates USB HID devices. Currently keyboard and mouse are implemented.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Usage Example

The Keyboard class sends keypress reports for a USB keyboard device to the host.

The Keycode class defines USB HID keycodes to send using Keyboard.

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()

The KeyboardLayoutUS sends ASCII characters using keypresses. It assumes the host is set to accept keypresses from a US keyboard.

If the host is expecting a non-US keyboard, the character to key mapping provided by KeyboardLayoutUS will not always be correct. Different keypresses will be needed in some cases. For instance, to type an 'A' on a French keyboard (AZERTY instead of QWERTY), Keycode.Q should be pressed.

Currently this package provides only KeyboardLayoutUS. More KeyboardLayout classes could be added to handle non-US keyboards and the different input methods provided by various operating systems.

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('$')

The Mouse class simulates a three-button mouse with a scroll wheel.

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)

The ConsumerControl class emulates consumer control devices such as remote controls, or the multimedia keys on certain keyboards.

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)

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Documentation

For information on building library documentation, please check out this guide.

Table of Contents

Simple test

Ensure your device works with this simple test.

examples/hid_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse

mouse = Mouse(usb_hid.devices)

# define buttons. these can be any physical switches/buttons, but the values
# here work out-of-the-box with a CircuitPlayground Express' A and B buttons.
up = digitalio.DigitalInOut(board.D4)
up.direction = digitalio.Direction.INPUT
up.pull = digitalio.Pull.DOWN

down = digitalio.DigitalInOut(board.D5)
down.direction = digitalio.Direction.INPUT
down.pull = digitalio.Pull.DOWN

while True:
    # scroll up one unit (varies with host/OS)
    if up.value:
        mouse.move(wheel=1)

    # scroll down one unit (varies with host/OS)
    elif down.value:
        mouse.move(wheel=-1)

    time.sleep(0.1)

Keyboard Shortcuts

Send ALT+Tab for swapping windows, and CTRL+K for searching in a browser.

examples/hid_keyboard_shortcuts.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)

# define buttons. these can be any physical switches/buttons, but the values
# here work out-of-the-box with a CircuitPlayground Express' A and B buttons.
swap = digitalio.DigitalInOut(board.D4)
swap.direction = digitalio.Direction.INPUT
swap.pull = digitalio.Pull.DOWN

search = digitalio.DigitalInOut(board.D5)
search.direction = digitalio.Direction.INPUT
search.pull = digitalio.Pull.DOWN

while True:
    # press ALT+TAB to swap windows
    if swap.value:
        kbd.send(Keycode.ALT, Keycode.TAB)

    # press CTRL+K, which in a web browser will open the search dialog
    elif search.value:
        kbd.send(Keycode.CONTROL, Keycode.K)

    time.sleep(0.1)

Simple Gamepad

Send gamepad buttons and joystick to the host.

examples/hid_simple_gamepad.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import digitalio
import analogio
import usb_hid

from gamepad import Gamepad

gp = Gamepad(usb_hid.devices)

# Create some buttons. The physical buttons are connected
# to ground on one side and these and these pins on the other.
button_pins = (board.D2, board.D3, board.D4, board.D5)

# Map the buttons to button numbers on the Gamepad.
# gamepad_buttons[i] will send that button number when buttons[i]
# is pushed.
gamepad_buttons = (1, 2, 8, 15)

buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]
for button in buttons:
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP

# Connect an analog two-axis joystick to A4 and A5.
ax = analogio.AnalogIn(board.A4)
ay = analogio.AnalogIn(board.A5)

# Equivalent of Arduino's map() function.
def range_map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min


while True:
    # Buttons are grounded when pressed (.value = False).
    for i, button in enumerate(buttons):
        gamepad_button_num = gamepad_buttons[i]
        if button.value:
            gp.release_buttons(gamepad_button_num)
            print(" release", gamepad_button_num, end="")
        else:
            gp.press_buttons(gamepad_button_num)
            print(" press", gamepad_button_num, end="")

    # Convert range[0, 65535] to -127 to 127
    gp.move_joysticks(
        x=range_map(ax.value, 0, 65535, -127, 127),
        y=range_map(ay.value, 0, 65535, -127, 127),
    )
    print(" x", ax.value, "y", ay.value)

HID Joywing

Use Joy FeatherWing to drive Gamepad.

examples/hid_joywing_gamepad.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Use Joy FeatherWing to drive Gamepad.
# https://www.adafruit.com/product/3632
# https://learn.adafruit.com/joy-featherwing

import time

import board
import busio
from micropython import const
import adafruit_seesaw
import usb_hid
from gamepad import Gamepad


def range_map(value, in_min, in_max, out_min, out_max):
    return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min


BUTTON_RIGHT = const(6)
BUTTON_DOWN = const(7)
BUTTON_LEFT = const(9)
BUTTON_UP = const(10)
BUTTON_SEL = const(14)
button_mask = const(
    (1 << BUTTON_RIGHT)
    | (1 << BUTTON_DOWN)
    | (1 << BUTTON_LEFT)
    | (1 << BUTTON_UP)
    | (1 << BUTTON_SEL)
)

i2c = busio.I2C(board.SCL, board.SDA)

ss = adafruit_seesaw.Seesaw(i2c)

ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)

last_game_x = 0
last_game_y = 0

g = Gamepad(usb_hid.devices)

while True:
    x = ss.analog_read(2)
    y = ss.analog_read(3)

    game_x = range_map(x, 0, 1023, -127, 127)
    game_y = range_map(y, 0, 1023, -127, 127)
    if last_game_x != game_x or last_game_y != game_y:
        last_game_x = game_x
        last_game_y = game_y
        print(game_x, game_y)
        g.move_joysticks(x=game_x, y=game_y)

    buttons = (BUTTON_RIGHT, BUTTON_DOWN, BUTTON_LEFT, BUTTON_UP, BUTTON_SEL)
    button_state = [False] * len(buttons)
    for i, button in enumerate(buttons):
        buttons = ss.digital_read_bulk(button_mask)
        if not (buttons & (1 << button) and not button_state[i]):
            g.press_buttons(i + 1)
            print("Press", i + 1)
            button_state[i] = True
        elif button_state[i]:
            g.release_buttons(i + 1)
            print("Release", i + 1)
            button_state[i] = False

    time.sleep(0.01)

Consumer Control Brightness

Send brightness up and down consumer codes to the host.

examples/hid_consumer_control_brightness.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import digitalio
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

cc = ConsumerControl(usb_hid.devices)

# define buttons. these can be any physical switches/buttons, but the values
# here work out-of-the-box with a FunHouse UP and DOWN buttons.
button_up = digitalio.DigitalInOut(board.BUTTON_UP)
button_up.switch_to_input(pull=digitalio.Pull.DOWN)

button_down = digitalio.DigitalInOut(board.BUTTON_DOWN)
button_down.switch_to_input(pull=digitalio.Pull.DOWN)

while True:
    if button_up.value:
        print("Button up pressed!")
        # send brightness up button press
        cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT)

    if button_down.value:
        print("Button down pressed!")
        # send brightness down button press
        cc.send(ConsumerControlCode.BRIGHTNESS_DECREMENT)

    time.sleep(0.1)

adafruit_hid.keyboard.Keyboard

  • Author(s): Scott Shawcroft, Dan Halbert
class adafruit_hid.keyboard.Keyboard(devices)[source]

Send HID keyboard reports.

LED_CAPS_LOCK = 2

LED Usage ID for Caps Lock

LED_COMPOSE = 8

LED Usage ID for Compose

LED_NUM_LOCK = 1

LED Usage ID for Num Lock

LED_SCROLL_LOCK = 4

LED Usage ID for Scroll Lock

led_on(led_code)[source]

Returns whether an LED is on based on the led code

Examples:

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

# Initialize Keybaord
kbd = Keyboard(usb_hid.devices)

# Press and release CapsLock.
kbd.press(Keycode.CAPS_LOCK)
time.sleep(.09)
kbd.release(Keycode.CAPS_LOCK)

# Check status of the LED_CAPS_LOCK
print(kbd.led_on(Keyboard.LED_CAPS_LOCK))
led_status

Returns the last received report

press(*keycodes)[source]

Send a report indicating that the given keys have been pressed.

Parameters:keycodes – Press these keycodes all at once.
Raises:ValueError – if more than six regular keys are pressed.

Keycodes may be modifiers or regular keys. No more than six regular keys may be pressed simultaneously.

Examples:

from adafruit_hid.keycode import Keycode

# Press ctrl-x.
kbd.press(Keycode.LEFT_CONTROL, Keycode.X)

# Or, more conveniently, use the CONTROL alias for LEFT_CONTROL:
kbd.press(Keycode.CONTROL, Keycode.X)

# Press a, b, c keys all at once.
kbd.press(Keycode.A, Keycode.B, Keycode.C)
release(*keycodes)[source]

Send a USB HID report indicating that the given keys have been released.

Parameters:keycodes – Release these keycodes all at once.

If a keycode to be released was not pressed, it is ignored.

Example:

# release SHIFT key
kbd.release(Keycode.SHIFT)
release_all()[source]

Release all pressed keys.

send(*keycodes)[source]

Press the given keycodes and then release all pressed keys.

Parameters:keycodes – keycodes to send together

adafruit_hid.keycode.Keycode

  • Author(s): Scott Shawcroft, Dan Halbert
class adafruit_hid.keycode.Keycode[source]

USB HID Keycode constants.

This list is modeled after the names for USB keycodes defined in https://usb.org/sites/default/files/hut1_21_0.pdf#page=83. This list does not include every single code, but does include all the keys on a regular PC or Mac keyboard.

Remember that keycodes are the names for key positions on a US keyboard, and may not correspond to the character that you mean to send if you want to emulate non-US keyboard. For instance, on a French keyboard (AZERTY instead of QWERTY), the keycode for ‘q’ is used to indicate an ‘a’. Likewise, ‘y’ represents ‘z’ on a German keyboard. This is historical: the idea was that the keycaps could be changed without changing the keycodes sent, so that different firmware was not needed for different variations of a keyboard.

A = 4

a and A

ALT = 226

Alias for LEFT_ALT; Alt is also known as Option (Mac)

APPLICATION = 101

Application: also known as the Menu key (Windows)

B = 5

b and B

BACKSLASH = 49

\ and |

BACKSPACE = 42

Delete backward (Backspace)

C = 6

c and C

CAPS_LOCK = 57

Caps Lock

COMMA = 54

, and <

COMMAND = 227

Labeled as Command on Mac keyboards, with a clover glyph

CONTROL = 224

Alias for LEFT_CONTROL

D = 7

d and D

DELETE = 76

Delete forward

DOWN_ARROW = 81

Move the cursor down

E = 8

e and E

EIGHT = 37

8 and *

END = 77

End (often moves to end of line)

ENTER = 40

Enter (Return)

EQUALS = 46

=` and ``+

ESCAPE = 41

Escape

F = 9

f and F

F1 = 58

Function key F1

F10 = 67

Function key F10

F11 = 68

Function key F11

F12 = 69

Function key F12

F13 = 104

Function key F13 (Mac)

F14 = 105

Function key F14 (Mac)

F15 = 106

Function key F15 (Mac)

F16 = 107

Function key F16 (Mac)

F17 = 108

Function key F17 (Mac)

F18 = 109

Function key F18 (Mac)

F19 = 110

Function key F19 (Mac)

F2 = 59

Function key F2

F20 = 111

Function key F20

F21 = 112

Function key F21

F22 = 113

Function key F22

F23 = 114

Function key F23

F24 = 115

Function key F24

F3 = 60

Function key F3

F4 = 61

Function key F4

F5 = 62

Function key F5

F6 = 63

Function key F6

F7 = 64

Function key F7

F8 = 65

Function key F8

F9 = 66

Function key F9

FIVE = 34

5 and %

FORWARD_SLASH = 56

/ and ?

FOUR = 33

4 and $

G = 10

g and G

GRAVE_ACCENT = 53

` and ~

GUI = 227

Alias for LEFT_GUI; GUI is also known as the Windows key, Command (Mac), or Meta

H = 11

h and H

HOME = 74

Home (often moves to beginning of line)

I = 12

i and I

INSERT = 73

Insert

J = 13

j and J

K = 14

k and K

KEYPAD_ASTERISK = 85

Keypad *

KEYPAD_BACKSLASH = 100

Keypad \ and | (Non-US)

KEYPAD_EIGHT = 96

Keypad 8 and Up Arrow

KEYPAD_ENTER = 88

Keypad Enter

KEYPAD_EQUALS = 103

Keypad = (Mac)

KEYPAD_FIVE = 93

Keypad 5

KEYPAD_FORWARD_SLASH = 84

Keypad /

KEYPAD_FOUR = 92

Keypad 4 and Left Arrow

KEYPAD_MINUS = 86

Keyapd -

KEYPAD_NINE = 97

Keypad 9 and PgUp

KEYPAD_NUMLOCK = 83

Num Lock (Clear on Mac)

KEYPAD_ONE = 89

Keypad 1 and End

KEYPAD_PERIOD = 99

Keypad . and Del

KEYPAD_PLUS = 87

Keypad +

KEYPAD_SEVEN = 95

Keypad 7 and Home

KEYPAD_SIX = 94

Keypad 6 and Right Arrow

KEYPAD_THREE = 91

Keypad 3 and PgDn

KEYPAD_TWO = 90

Keypad 2 and Down Arrow

KEYPAD_ZERO = 98

Keypad 0 and Ins

L = 15

l and L

LEFT_ALT = 226

Alt modifier left of the spacebar

LEFT_ARROW = 80

Move the cursor left

LEFT_BRACKET = 47

[ and {

LEFT_CONTROL = 224

Control modifier left of the spacebar

LEFT_GUI = 227

GUI modifier left of the spacebar

LEFT_SHIFT = 225

Shift modifier left of the spacebar

M = 16

m and M

MINUS = 45

-` and ``_

N = 17

n and N

NINE = 38

9 and (

O = 18

o and O

ONE = 30

1 and !

OPTION = 226

Labeled as Option on some Mac keyboards

P = 19

p and P

PAGE_DOWN = 78

Go forward one page

PAGE_UP = 75

Go back one page

PAUSE = 72

Pause (Break)

PERIOD = 55

. and >

POUND = 50

# and ~ (Non-US keyboard)

POWER = 102

Power (Mac)

PRINT_SCREEN = 70

Print Screen (SysRq)

Q = 20

q and Q

QUOTE = 52

' and "

R = 21

r and R

RETURN = 40

Alias for ENTER

RIGHT_ALT = 230

Alt modifier right of the spacebar

RIGHT_ARROW = 79

Move the cursor right

RIGHT_BRACKET = 48

] and }

RIGHT_CONTROL = 228

Control modifier right of the spacebar

RIGHT_GUI = 231

GUI modifier right of the spacebar

RIGHT_SHIFT = 229

Shift modifier right of the spacebar

S = 22

s and S

SCROLL_LOCK = 71

Scroll Lock

SEMICOLON = 51

; and :

SEVEN = 36

7 and &

SHIFT = 225

Alias for LEFT_SHIFT

SIX = 35

6 and ^

SPACE = 44

Alias for SPACEBAR

SPACEBAR = 44

Spacebar

T = 23

t and T

TAB = 43

Tab and Backtab

THREE = 32

3 and #

TWO = 31

2 and @

U = 24

u and U

UP_ARROW = 82

Move the cursor up

V = 25

v and V

W = 26

w and W

WINDOWS = 227

Labeled with a Windows logo on Windows keyboards

X = 27

x and X

Y = 28

y and Y

Z = 29

z and Z

ZERO = 39

0 and )

classmethod modifier_bit(keycode)[source]

Return the modifer bit to be set in an HID keycode report if this is a modifier key; otherwise return 0.

adafruit_hid.keyboard_layout_us.KeyboardLayoutUS

  • Author(s): Dan Halbert
class adafruit_hid.keyboard_layout_us.KeyboardLayoutUS(keyboard)[source]

Map ASCII characters to appropriate keypresses on a standard US PC keyboard.

Non-ASCII characters and most control characters will raise an exception.

keycodes(char)[source]

Return a tuple of keycodes needed to type the given character.

Parameters:char (str of length one.) – A single ASCII character in a string.
Returns:tuple of Keycode keycodes.
Raises:ValueError – if char is not ASCII or there is no keycode for it.

Examples:

# Returns (Keycode.TAB,)
keycodes('  ')
# Returns (Keycode.A,)
keycode('a')
# Returns (Keycode.SHIFT, Keycode.A)
keycode('A')
# Raises ValueError because it's a accented e and is not ASCII
keycode('é')
write(string)[source]

Type the string by pressing and releasing keys on my keyboard.

Parameters:string – A string of ASCII characters.
Raises:ValueError – if any of the characters are not ASCII or have no keycode (such as some control characters).

Example:

# Write abc followed by Enter to the keyboard
layout.write('abc\n')

adafruit_hid.mouse.Mouse

  • Author(s): Dan Halbert
class adafruit_hid.mouse.Mouse(devices)[source]

Send USB HID mouse reports.

LEFT_BUTTON = 1

Left mouse button.

MIDDLE_BUTTON = 4

Middle mouse button.

RIGHT_BUTTON = 2

Right mouse button.

click(buttons)[source]

Press and release the given mouse buttons.

Parameters:buttons – a bitwise-or’d combination of LEFT_BUTTON, MIDDLE_BUTTON, and RIGHT_BUTTON.

Examples:

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

# Double-click the left button.
m.click(Mouse.LEFT_BUTTON)
m.click(Mouse.LEFT_BUTTON)
move(x=0, y=0, wheel=0)[source]

Move the mouse and turn the wheel as directed.

Parameters:
  • x – Move the mouse along the x axis. Negative is to the left, positive is to the right.
  • y – Move the mouse along the y axis. Negative is upwards on the display, positive is downwards.
  • wheel – Rotate the wheel this amount. Negative is toward the user, positive is away from the user. The scrolling effect depends on the host.

Examples:

# Move 100 to the left. Do not move up and down. Do not roll the scroll wheel.
m.move(-100, 0, 0)
# Same, with keyword arguments.
m.move(x=-100)

# Move diagonally to the upper right.
m.move(50, 20)
# Same.
m.move(x=50, y=-20)

# Roll the mouse wheel away from the user.
m.move(wheel=1)
press(buttons)[source]

Press the given mouse buttons.

Parameters:buttons – a bitwise-or’d combination of LEFT_BUTTON, MIDDLE_BUTTON, and RIGHT_BUTTON.

Examples:

# Press the left button.
m.press(Mouse.LEFT_BUTTON)

# Press the left and right buttons simultaneously.
m.press(Mouse.LEFT_BUTTON | Mouse.RIGHT_BUTTON)
release(buttons)[source]

Release the given mouse buttons.

Parameters:buttons – a bitwise-or’d combination of LEFT_BUTTON, MIDDLE_BUTTON, and RIGHT_BUTTON.
release_all()[source]

Release all the mouse buttons.

adafruit_hid.consumer_control.ConsumerControl

  • Author(s): Dan Halbert
class adafruit_hid.consumer_control.ConsumerControl(devices)[source]

Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc.

press(consumer_code)[source]

Send a report to indicate that the given key has been pressed. Only one consumer control action can be pressed at a time, so any one that was previously pressed will be released.

Parameters:consumer_code – a 16-bit consumer control code.

Examples:

from adafruit_hid.consumer_control_code import ConsumerControlCode

# Raise volume for 0.5 seconds
consumer_control.press(ConsumerControlCode.VOLUME_INCREMENT)
time.sleep(0.5)
consumer_control.release()
release()[source]

Send a report indicating that the consumer control key has been released. Only one consumer control key can be pressed at a time.

Examples:

from adafruit_hid.consumer_control_code import ConsumerControlCode

# Raise volume for 0.5 seconds
consumer_control.press(ConsumerControlCode.VOLUME_INCREMENT)
time.sleep(0.5)
consumer_control.release()
send(consumer_code)[source]

Send a report to do the specified consumer control action, and then stop the action (so it will not repeat).

Parameters:consumer_code – a 16-bit consumer control code.

Examples:

from adafruit_hid.consumer_control_code import ConsumerControlCode

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

# Advance to next track (song).
consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK)

adafruit_hid.consumer_control_code.ConsumerControlCode

  • Author(s): Dan Halbert
class adafruit_hid.consumer_control_code.ConsumerControlCode[source]

USB HID Consumer Control Device constants.

This list includes a few common consumer control codes from https://www.usb.org/sites/default/files/hut1_21_0.pdf#page=118.

BRIGHTNESS_DECREMENT = 112

Decrease Brightness

BRIGHTNESS_INCREMENT = 111

Increase Brightness

EJECT = 184

Eject

FAST_FORWARD = 179

Fast Forward

MUTE = 226

Mute

PLAY_PAUSE = 205

Play/Pause toggle

RECORD = 178

Record

REWIND = 180

Rewind

SCAN_NEXT_TRACK = 181

Skip to next track

SCAN_PREVIOUS_TRACK = 182

Go back to previous track

STOP = 183

Stop

VOLUME_DECREMENT = 234

Decrease volume

VOLUME_INCREMENT = 233

Increase volume

Indices and tables