EasyManuals Logo

Adafruit Feather M4 Express User Manual

Adafruit Feather M4 Express
178 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #155 background imageLoading...
Page #155 background image
CircuitPython HID Keyboard and Mouse
One of the things we baked into CircuitPython is 'HID' (Human Interface Device) control - that means keyboard and
mouse capabilities. This means your CircuitPython board can act like a keyboard device and press key commands, or a
mouse and have it move the mouse pointer around and press buttons. This is really handy because even if you cannot
adapt your software to work with hardware, there's almost always a keyboard interface - so if you want to have a
capacitive touch interface for a game, say, then keyboard emulation can often get you going really fast!
This section walks you through the code to create a keyboard or mouse emulator. First we'll go through an example
that uses pins on your board to emulate keyboard input. Then, we will show you how to wire up a joystick to act as a
mouse, and cover the code needed to make that happen.
You'll need the adafruit_hid library folder if you don't already have it in your /lib folder! You can get it from the
CircuitPython Library Bundle (https://adafru.it/y8E). If you need help installing the library, check out the CircuitPython
Libraries page (https://adafru.it/ABU).
CircuitPython Keyboard Emulator
Copy and paste the code into code.py using your favorite editor, and save the file.
# CircuitPython demo - Keyboard emulator
import time
import board
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# A simple neat keyboard demo in CircuitPython
# The pins we'll use, each will have an internal pullup
keypress_pins = [board.A1, board.A2]
# Our array of key objects
key_pin_array = []
# The Keycode sent for each button, will be paired with a control key
keys_pressed = [Keycode.A, "Hello World!\n"]
control_key = Keycode.SHIFT
# The keyboard object!
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard()
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
# Make all pin objects inputs with pullups
for pin in keypress_pins:
key_pin = digitalio.DigitalInOut(pin)
key_pin.direction = digitalio.Direction.INPUT
key_pin.pull = digitalio.Pull.UP
key_pin_array.append(key_pin)
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
print("Waiting for key pin...")
© Adafruit Industries https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51 Page 160 of 183

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Adafruit Feather M4 Express and is the answer not in the manual?

Adafruit Feather M4 Express Specifications

General IconGeneral
BrandAdafruit
ModelFeather M4 Express
CategoryMicrocontrollers
LanguageEnglish

Related product manuals