Introduction
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.
Additional Layouts
This library has an en-US layout. Please check out and expand the library from Neradoc for additional layouts.
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)
Documentation
API documentation for this library can be found on Read the Docs.
For information on building library documentation, please check out this guide.
Contributing
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.
Table of Contents
adafruit_hid.keyboard.Keyboard
Keyboard
adafruit_hid.keycode.Keycode
Keycode
Keycode.A
Keycode.ALT
Keycode.APPLICATION
Keycode.B
Keycode.BACKSLASH
Keycode.BACKSPACE
Keycode.C
Keycode.CAPS_LOCK
Keycode.COMMA
Keycode.COMMAND
Keycode.CONTROL
Keycode.D
Keycode.DELETE
Keycode.DOWN_ARROW
Keycode.E
Keycode.EIGHT
Keycode.END
Keycode.ENTER
Keycode.EQUALS
Keycode.ESCAPE
Keycode.F
Keycode.F1
Keycode.F10
Keycode.F11
Keycode.F12
Keycode.F13
Keycode.F14
Keycode.F15
Keycode.F16
Keycode.F17
Keycode.F18
Keycode.F19
Keycode.F2
Keycode.F20
Keycode.F21
Keycode.F22
Keycode.F23
Keycode.F24
Keycode.F3
Keycode.F4
Keycode.F5
Keycode.F6
Keycode.F7
Keycode.F8
Keycode.F9
Keycode.FIVE
Keycode.FORWARD_SLASH
Keycode.FOUR
Keycode.G
Keycode.GRAVE_ACCENT
Keycode.GUI
Keycode.H
Keycode.HOME
Keycode.I
Keycode.INSERT
Keycode.J
Keycode.K
Keycode.KEYPAD_ASTERISK
Keycode.KEYPAD_BACKSLASH
Keycode.KEYPAD_EIGHT
Keycode.KEYPAD_ENTER
Keycode.KEYPAD_EQUALS
Keycode.KEYPAD_FIVE
Keycode.KEYPAD_FORWARD_SLASH
Keycode.KEYPAD_FOUR
Keycode.KEYPAD_MINUS
Keycode.KEYPAD_NINE
Keycode.KEYPAD_NUMLOCK
Keycode.KEYPAD_ONE
Keycode.KEYPAD_PERIOD
Keycode.KEYPAD_PLUS
Keycode.KEYPAD_SEVEN
Keycode.KEYPAD_SIX
Keycode.KEYPAD_THREE
Keycode.KEYPAD_TWO
Keycode.KEYPAD_ZERO
Keycode.L
Keycode.LEFT_ALT
Keycode.LEFT_ARROW
Keycode.LEFT_BRACKET
Keycode.LEFT_CONTROL
Keycode.LEFT_GUI
Keycode.LEFT_SHIFT
Keycode.M
Keycode.MINUS
Keycode.N
Keycode.NINE
Keycode.O
Keycode.ONE
Keycode.OPTION
Keycode.P
Keycode.PAGE_DOWN
Keycode.PAGE_UP
Keycode.PAUSE
Keycode.PERIOD
Keycode.POUND
Keycode.POWER
Keycode.PRINT_SCREEN
Keycode.Q
Keycode.QUOTE
Keycode.R
Keycode.RETURN
Keycode.RIGHT_ALT
Keycode.RIGHT_ARROW
Keycode.RIGHT_BRACKET
Keycode.RIGHT_CONTROL
Keycode.RIGHT_GUI
Keycode.RIGHT_SHIFT
Keycode.S
Keycode.SCROLL_LOCK
Keycode.SEMICOLON
Keycode.SEVEN
Keycode.SHIFT
Keycode.SIX
Keycode.SPACE
Keycode.SPACEBAR
Keycode.T
Keycode.TAB
Keycode.THREE
Keycode.TWO
Keycode.U
Keycode.UP_ARROW
Keycode.V
Keycode.W
Keycode.WINDOWS
Keycode.X
Keycode.Y
Keycode.Z
Keycode.ZERO
Keycode.modifier_bit()
adafruit_hid.keyboard_layout_us.KeyboardLayoutUS
KeyboardLayout
KeyboardLayoutUS
adafruit_hid.keyboard_layout_base.KeyboardLayoutBase
KeyboardLayoutBase
KeyboardLayoutBase.ALTGR_FLAG
KeyboardLayoutBase.ASCII_TO_KEYCODE
KeyboardLayoutBase.COMBINED_KEYS
KeyboardLayoutBase.HIGHER_ASCII
KeyboardLayoutBase.NEED_ALTGR
KeyboardLayoutBase.RIGHT_ALT_CODE
KeyboardLayoutBase.SHIFT_CODE
KeyboardLayoutBase.SHIFT_FLAG
KeyboardLayoutBase.keycodes()
KeyboardLayoutBase.write()
adafruit_hid.mouse.Mouse
Mouse
adafruit_hid.consumer_control.ConsumerControl
ConsumerControl
adafruit_hid.consumer_control_code.ConsumerControlCode
ConsumerControlCode
ConsumerControlCode.BRIGHTNESS_DECREMENT
ConsumerControlCode.BRIGHTNESS_INCREMENT
ConsumerControlCode.EJECT
ConsumerControlCode.FAST_FORWARD
ConsumerControlCode.MUTE
ConsumerControlCode.PLAY_PAUSE
ConsumerControlCode.RECORD
ConsumerControlCode.REWIND
ConsumerControlCode.SCAN_NEXT_TRACK
ConsumerControlCode.SCAN_PREVIOUS_TRACK
ConsumerControlCode.STOP
ConsumerControlCode.VOLUME_DECREMENT
ConsumerControlCode.VOLUME_INCREMENT