Skip to content

Instantly share code, notes, and snippets.

@loretod
Created August 22, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loretod/359cb73b0527c33fe0f1584c886bb008 to your computer and use it in GitHub Desktop.
Save loretod/359cb73b0527c33fe0f1584c886bb008 to your computer and use it in GitHub Desktop.
A Circuitpython gist for moving a mouse using button presses on a TrinketMO.
"""Button Based Mouse Navigation using the Adafruit Trinket MO board"""
#Import standard libraries
import time
import board
from digitalio import DigitalInOut, Direction, Pull
import usb_hid
#Import the mouse libaray from adafruit_hid folder
from adafruit_hid.mouse import Mouse
#Indicate pins being used
upButton = DigitalInOut(board.D0)
downButton = DigitalInOut(board.D1)
rightButton = DigitalInOut(board.D2)
leftButton = DigitalInOut(board.D3)
mouseClick = DigitalInOut(board.D4)
#Indicate that the pins will be used as input
upButton.direction = Direction.INPUT
downButton.direction = Direction.INPUT
rightButton.direction = Direction.INPUT
leftButton.direction = Direction.INPUT
mouseClick.direction = Direction.INPUT
#Indicate the pins will start in the pull up position
upButton.pull = Pull.UP
downButton.pull = Pull.UP
rightButton.pull = Pull.UP
leftButton.pull = Pull.UP
mouseClick.pull = Pull.UP
#start the mouse instance and print a serial command that things are ready to go.
mouse = Mouse(usb_hid.devices)
print("Waiting for key pin...")
#Loop through and check for pin position changes. Send a keycode combination if condition is met.
while True:
if not upButton.value:
mouse.move(x=10)
print('up')
if not downButton.value:
mouse.move(x=-10)
print('Down')
if not rightButton.value:
mouse.move(y=10)
print('right')
if not leftButton.value:
mouse.move(y=-10)
print('left')
if not mouseClick.value:
mouse.click(Mouse.LEFT_BUTTON)
time.sleep(0.2) # Debounce delay
#Short debounce pause after keypress to slow a key repeat rate. Modify as needed.
time.sleep(.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment