Skip to content

[Bug]: macOS backend reports wrong button for button_release_event#31937

Open
mishanilkazreen wants to merge 1 commit into
matplotlib:mainfrom
mishanilkazreen:mishanilkazreen/issue31899
Open

[Bug]: macOS backend reports wrong button for button_release_event#31937
mishanilkazreen wants to merge 1 commit into
matplotlib:mainfrom
mishanilkazreen:mishanilkazreen/issue31899

Conversation

@mishanilkazreen

Copy link
Copy Markdown

Fixes #31899

PR summary

The issue reports on macOS that Option+click and Ctrl+click were not correctly mapped to middle and right button clicks, respectively.

In qtagg, Option+click was hardcoded to button = 1 as backend_qt.py did not contain a button modifier mapping. So event.button() returned the physical button, which resulted in Option+click incorrectly emulating button 1 instead of button 2.

In macosx, the mouseDown: method was correctly mapped, however, mouseUp: hardcoded button = 1, which resulted in press and release being inconsistent.

Fixes

For qtagg, I added a _remap_darwin_button(...) static method in backend_qt.py that maps Alt/Option+left-click to the middle button on macOS.

For macosx, I added a static variable effectiveLeftButton to store the resolved button at press time, allowing mouseUp: to read it back instead of hardcoding 1.

Minimum self-contained example

Instructions:

  • Left Click
  • Left Click + Option
  • Left Click + Control
import matplotlib
matplotlib.use('macosx') # macosx/qtagg
import matplotlib.pyplot as plt

def on_press(event):
    print(f'button press:   {event.button}')

def on_release(event):
    print(f'button release: {event.button}')
    print()

fig = plt.figure()
fig.canvas.mpl_connect('button_press_event', on_press)
fig.canvas.mpl_connect('button_release_event', on_release)
plt.show()

Outputs

Current

macosx

button press:   1
button release: 1

button press:   2
button release: 1

button press:   3
button release: 1

qtagg (PyQt6)

button press:   1
button release: 1

button press:   1
button release: 1

button press:   3
button release: 3

With Fixes

macosx & qtagg

button press:   1
button release: 1

button press:   2
button release: 2

button press:   3
button release: 3

Question

As raised in the initial issue, should the intended behaviour for the action Alt + left-click result in the middle mouse? This implements it, but can be dropped by removing _remap_darwin_button(...) helper function.

AI Disclosure

Claude Opus 4.8 was utilised to proofread my changes.

PR checklist

@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@iccir

iccir commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for this! Your solution for the macOS backend made me realize a potential issue that I forgot to check in the original bug.

Consider the following:

  1. Hold down Alt/Option
  2. Press the mouse button, but don't release
  3. Release Alt/Option
  4. Release mouse button

qtagg with your PR prints:

button press: 2
button release: 1

If you do the same with Control instead of Alt/Option, qtagg instead prints:

button press: 3
button release: 3

I suspect that we want Alt/Option to show 2 for both press and release to match what QT already does for the right mouse button, but I'm new here so I'm not sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: macOS backend reports wrong button for button_release_event

2 participants