![Manicured hand holding Trinket M0.](https://cdn-shop.adafruit.com/640x480/3500-04.jpg)
import board import digitalio import time import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode class ArcadeButton: def __init__(self, key_code, pin): self.key_code = key_code self.pin = digitalio.DigitalInOut(pin) self.pin.direction = digitalio.Direction.INPUT self.pin.pull = digitalio.Pull.UP self._state = False @property def is_pressed(self): return self._state @property def state(self): return self._state @state.setter def state(self, new_state): if self._state != new_state: self._state = new_state if new_state: # Button was just pressed self.simulate_key() def simulate_key(self): kbd = Keyboard(usb_hid.devices) kbd.press(self.key_code) kbd.release_all() class ArcadeButtons: def __init__(self, *buttons): self.buttons = buttons def update_states(self): for button in self.buttons: button.state = button.pin.value # Create instances of the ArcadeButton class for each button esc_button = ArcadeButton(Keycode.ESCAPE, board.D0) tab_button = ArcadeButton(Keycode.TAB, board.D1) enter_button = ArcadeButton(Keycode.ENTER, board.D2) # Create an instance of the ArcadeButtons class all_buttons = ArcadeButtons(esc_button, tab_button, enter_button) # Main loop while True: # Update the states of all buttons all_buttons.update_states() time.sleep(0.01) # Add a small delay to prevent rapid detection