Skip to content

Instantly share code, notes, and snippets.

@BenjaminSantiago
Created November 21, 2022 18:59
Show Gist options
  • Save BenjaminSantiago/4cf34c7360f08c93271ca9ed9fb2b267 to your computer and use it in GitHub Desktop.
Save BenjaminSantiago/4cf34c7360f08c93271ca9ed9fb2b267 to your computer and use it in GitHub Desktop.
our keyboard example combining Processing and the CPX; the CPX side
# our imports
# ------------------------------------------------------>
import usb_hid
"""
note that, this HID functionality is unique to the
circuit python express (there is an older "non-express"
version that is discontinued). Because of this we import
cpx, rather than cp.
"""
from adafruit_circuitplayground.express import cpx
"""
use this to act as the keyboard
this is where the "send" commands from the
CPX to the computer come from
"""
from adafruit_hid.keyboard import Keyboard
"""
these are all the codes we use to give
the specific "code" of the key we want to
pretend to press. If we didn't have this
we'd have to refer to a table of numbers
or something like this.
"""
from adafruit_hid.keycode import Keycode
# ------------------------------------------------------>
# our keyboard object
# this does some setting up
kbd = Keyboard(usb_hid.devices)
# ------------------------------------------------------>
while True:
# if we press button a, send an O or an o
# the switch controls the shift key
# ----------------------------------->
if cpx.button_a:
if cpx.switch:
kbd.send(Keycode.SHIFT, Keycode.A)
else:
kbd.send(Keycode.A)
while cpx.button_a:
# pass means do nothing
# we just need to do this
# to prevent multiple undesired
# keypresses.
pass
# if we press button b,
# send an X or an x, depending
# on whether the switch is "on"
# ----------------------------------->
if cpx.button_b:
if cpx.switch:
kbd.send(Keycode.SHIFT, Keycode.B)
else:
kbd.send(Keycode.B)
while cpx.button_b:
pass
# ------------------------------------------------------>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment