Skip to content

Conversation

RetiredWizard
Copy link
Contributor

@RetiredWizard RetiredWizard commented Sep 27, 2025

While adding a "chording" functionality to the minesweeper application I realized that the USB_Host_Mouse library didn't provide a way of determining if a mouse button was being held down or detecting when it is released.

By making the pressed_btns variable an accessible class property the calling application can now determine the current state of the button pushes after each call to the update method.

The start of the mouse update loop for minesweeper would be modified to the following which allows both the x,y coordinates and the current mouse button status to be monitored independently:

mouse_tg = mouse.tilegrid

while True:
    update_ui()
    # update cursor position, and check for clicks
    mouse.update()
    buttons = mouse.pressed_btns

    # Extract button states
    if buttons is None or buttons == []:
        left_button = 0
        right_button = 0
    else:
        left_button = 1 if 'left' in buttons else 0
        right_button = 1 if 'right' in buttons else 0

    mouse_coords = (mouse_tg.x, mouse_tg.y)

Edit: Without these changes, if there is no mouse data (the mouse isn't moved and the button status doesn't change) when the update method is called a value of None is returned for the button values and the current state of the buttons is lost.

Edit: If the application is only interested in capturing button pushes and not the held or released status then the return value can be used as it had been before this update.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you!

@FoamyGuy FoamyGuy merged commit d214ace into adafruit:main Sep 29, 2025
1 check passed
@RetiredWizard RetiredWizard deleted the usbmousebtns branch September 29, 2025 17:51
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants