Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Separately track modifier keys for mouse events. #23473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2022

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jul 22, 2022

PR Summary

Ensure that modifier keys are correctly reported for mouse events even if the last key press occurred out of the canvas.

Whether the event modifiers are directly available on enter/leave events
depends on the backend, but all are handled here (except possibly for
macos, which I haven't checked).

There's a first separate commit for the macosx backend for some preliminary work with ObjC.

Followup to #16931 (it's the reason for that big PR...). Supersedes #6159. See also the side note at #23436 (comment).

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@greglucas
Copy link
Contributor

I haven't had a chance to look at the details here, but I just pulled and tested quick on macosx and hit some errors that you can probably fix pretty quick:

Clicking anywhere:

TypeError: MouseEvent.__init__() got an unexpected keyword argument 'num'

ctrl + clicking anywhere:

SystemError: NULL object passed to Py_BuildValue

@anntzer
Copy link
Contributor Author

anntzer commented Jul 29, 2022

Oops, thanks for trying. Hopefully fixed now?

@greglucas
Copy link
Contributor

Looks like the failures with macosx are real and a PROCESS_EVENT snuck in after your initial PR.

@anntzer
Copy link
Contributor Author

anntzer commented Dec 13, 2022

Oops, probably a careless rebase, thanks for the ping.

Copy link
Contributor

@greglucas greglucas left a comment

Choose a reason for hiding this comment

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

tk seems to be double registering the modifiers in key_press now. (I see shift+shift and maybe more illuminating is ctrl+control rather than control+control although I only expect a single control).

The modifiers appear with the mouse presses as expected in macosx, qt, tk. However, I think it'd be nice to also add these to the keypress events because currently it is only an empty frozenset on those events.

Test script I was using:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1)
fig.canvas.mpl_connect('key_press_event', lambda key_event: print(f'press {key_event.key!r}'))
fig.canvas.mpl_connect('key_release_event', lambda key_event: print(f'release {key_event.key!r}'))
fig.canvas.mpl_connect('key_press_event', lambda key_event: print(f'modifiers press {key_event.modifiers!r}'))
fig.canvas.mpl_connect('button_press_event', lambda key_event: print(f'modifiers press {key_event.modifiers!r}'))
plt.show()

@anntzer
Copy link
Contributor Author

anntzer commented Dec 13, 2022

Ah, I had forgotten to implement the exclude kwarg to _mpl_modifiers for some backends, thanks for catching that, the doubling of modifiers should now be fixed.
Can I ask for a joker and defer adding modifiers to key events to another issue?

Copy link
Contributor

@greglucas greglucas left a comment

Choose a reason for hiding this comment

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

Fine to save some work for follow-ups and break this up. This is a good start!

Works as expected for me now, but there are some differences between backends which is unrelated to this PR, but it might be nice to fix up the inconsistencies later. It is likely related to mac's keyboards.

macosx -> good
qt5 -> good
tk -> Pressing command, I get a KeyPress "super+meta" (KeyRelease is only "meta" which is odd), but only "super" in the modifiers. I would expect "cmd" in the modifiers.
gtk3 -> Pressing command, I get a KeyPress "meta" but nothing in the modifiers when I expect a "cmd" in the modifiers

@anntzer
Copy link
Contributor Author

anntzer commented Dec 14, 2022

I suspect it's just a matter of updating (if needed) the table in _mpl_modifiers (previously _get_key) (perhaps by manually inspecting event.state in various cases); as you have access to a mac, can you give it a try (perhaps both with a laptop keyboard and with an independent one just to be sure)?

@tacaswell tacaswell added this to the v3.7.0 milestone Dec 15, 2022
@tacaswell
Copy link
Member

I tested qt + tk on linux and osx on osx.

Also saw something funny going on with 'meta' on tk. I saw it in the out put from the snippet Greg posted, but I could not sort out which keys I hit to get it.

@tacaswell
Copy link
Member

Anyone can merge on green (again, excluding the patch coverage).

Whether the event modifiers are directly available on enter/leave events
depends on the backend, but all are handled here (except possibly for
macos, which I haven't checked).
@tacaswell tacaswell merged commit eb92923 into matplotlib:main Dec 16, 2022
@tacaswell
Copy link
Member

Thank you @anntzer !

@anntzer anntzer deleted the mouse-modifiers branch December 16, 2022 17:47
@anntzer
Copy link
Contributor Author

anntzer commented Jan 31, 2023

@greglucas I got my hands on a mac :) and had a look at #23473 (review).
I think the relevant patch to fix the super+meta issue on tk is basically

diff --git i/lib/matplotlib/backends/_backend_tk.py w/lib/matplotlib/backends/_backend_tk.py
index 7aa24e06ff..6893120ace 100644
--- i/lib/matplotlib/backends/_backend_tk.py
+++ w/lib/matplotlib/backends/_backend_tk.py
@@ -354,7 +354,7 @@ class FigureCanvasTk(FigureCanvasBase):
             ("ctrl", 1 << 2, "control"),
             ("alt", 1 << 4, "alt"),
             ("shift", 1 << 0, "shift"),
-            ("super", 1 << 3, "super"),
+            ("foobar", 1 << 3, "meta"),
         ] if sys.platform == "darwin" else [
             ("ctrl", 1 << 2, "control"),
             ("alt", 1 << 3, "alt"),

where the last tuple item must be "meta" (to match tk reporting the cmd key with event.keysym = "Meta_L" (or "Meta_R")) and the first entry is how you think the modifier should actually be called (I guess "cmd", or perhaps "meta", I don't really have a good intuition here)? Can you decide on that last point and pick up the patch?

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.

3 participants