Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MNT: Add modifier key press handling to macosx backend#21512

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
greglucas merged 5 commits intomatplotlib:mainfromgreglucas:macosx-keypress
Nov 30, 2021

Conversation

greglucas
Copy link
Contributor

@greglucasgreglucas commentedOct 31, 2021
edited
Loading

PR Summary

The flagsChanged event handles single presses of modifier keys, so we need to send the corresponding string to our key press handlers from within that routine. Modifier + second key is handled within the KeyUp/KeyDown routines already, so leave those alone.

Taking the example from issue#20486

importmatplotlib.pyplotaspltfig=plt.figure()defon_key(event):print('you pressed',event.key,event.xdata,event.ydata)defon_key_release(event):print('you released',event.key,event.xdata,event.ydata)fig.canvas.mpl_connect('key_press_event',on_key)fig.canvas.mpl_connect('key_release_event',on_key_release)plt.show()

this adds the single modifier key presses back in. Before this, the (modifier + key) "cmd+a" would show up, but there wasn't a single "cmd" (modifier) preceding it.

you pressed cmd None Noneyou pressed cmd+a None None

I'm not sure how to send key press events through the terminal natively, so there are no automated tests.

closes#20486
closes#9837

PR Checklist

  • Has pytest style unit tests (andpytest passes).
  • IsFlake 8 compliant (runflake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).
  • Conforms to Matplotlib style conventions (installflake8-docstrings and runflake8 --docstring-convention=all).
  • New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).

@greglucasgreglucas added OS: Apple GUI: MacOSX PR: bugfixPull requests that fix identified bugs labelsOct 31, 2021
@greglucasgreglucas added this to thev3.6.0 milestoneOct 31, 2021
Copy link
Member

@jklymakjklymak left a comment

Choose a reason for hiding this comment

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

This works as advertised. Not an expert on macOS backend, so not sure this is the "right" fix, but....

src/_macosx.m Outdated
match up with what the front-end and does the front-end even handle modifier keys by themselves?

// flagsChanged gets called on single modifier keypresses
// The modifier + second key gets handled in convertKeyEvent in KeyUp/KeyDown
Copy link
Contributor

Choose a reason for hiding this comment

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

does it also work for e.g. "ctrl+shift"? (dunno, just asking)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

a ctrl, shift, f for example produces this:

youpressedcmdNoneNoneyoupressedshiftNoneNoneyoupressedcmd+FNoneNoneyoupressedcmdNoneNone

I think the extra command at the end is there due to the shift also being let up and changing state while command is still pressed...

Copy link
Contributor

Choose a reason for hiding this comment

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

On other backends, the second press is "cmd+shift", not just shift. (Well, ctrl+shift on non-osx.)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

OK, so this was more complicated than I had hoped... The flagsChanged gets called on any press OR release, which is why I was printing out "pressed" when it was really a "release".

I think this means we have to keep track of the state of the keys ourselves and what is going up/down. I think this now matches what other backends do, including press/release properly.

@greglucasgreglucasforce-pushed themacosx-keypress branch 2 times, most recently fromfc77052 to2120ca0CompareNovember 20, 2021 14:40
src/_macosx.m Outdated
lastCommand = false;
keyChangeCommand = true;
keyrelease = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a huge deal, but I guess each of these pairs can be grouped to

bool isPress =true;// false for releaseif ([eventmodifierFlags] & NSEventModifierFlagCommand) {    lastCommand = !lastCommand    keyChangeCommand =true    isPress = lastCommand;}elseif (...)    ...}else {// Unknown flagreturn;}

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

NSEventModifierFlagCommand will only be true when it is held down, so we would miss the release with that implementation I think... We could probably squash the two cases into a single if block with your suggestion, but maybe I'm missing a more elegant way to keep track of the push/release here:

if ((([eventmodifierFlags] & NSEventModifierFlagCommand) && !lastCommand) ||        (!([eventmodifierFlags] & NSEventModifierFlagCommand) && lastCommand)) {// Command pressed or released        lastCommand = !lastCommand;        keyChangeCommand =true;        keypress = lastCommand;    } ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sorry, I missed the inversion. Perhaps a good place for a^ (xor)?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

👍 Yep, a good place for it, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM, although probably another mac user should test :-)

@anntzer
Copy link
Contributor

anntzer commentedNov 26, 2021
edited
Loading

@greglucas
Copy link
ContributorAuthor

Good suggestion about updating the table.

  • The shift + 2 on US keyboards is@, so I'm not sure whether the" for the other backends are expected or not.
  • I don't have an altGr key, so I can't check that.
  • I don't have a capital A key, I'm not entirely clear what that entry is referring to? Possibly pressing a with caps_lock on...

@anntzer
Copy link
Contributor

The shift + 2 on US keyboards is @, so I'm not sure whether the " for the other backends are expected or not.

Looks like the original author was from Mexico where shift+2 is " (http://kbdlayout.info/KBDLA/), probably the entry should be fixed for qwerty.

I don't have an altGr key, so I can't check that.

Perhttps://en.wikipedia.org/wiki/AltGr_key I guess that's "left alt/option"

I don't have a capital A key, I'm not entirely clear what that entry is referring to? Possibly pressing a with caps_lock on...

I guess no one has, probably that was capslocked indeed?

greglucas reacted with thumbs up emoji

@anntzer
Copy link
Contributor

Thanks for the table update. I'll merge in a day or two if the macos crew doesn't manifest themselves for a second review :)

@jklymak
Copy link
Member

I've not been following. Has it diverged significantly from when I reviewed? I can try it again.

@anntzer
Copy link
Contributor

Mostly just check that things work just as advertised in the table under event_handling.rst.

Copy link
Contributor

@anntzeranntzer left a comment

Choose a reason for hiding this comment

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

modulo@jklymak checking, per the above.

@greglucas
Copy link
ContributorAuthor

There was quite a bit of logic rework since your initial review, and this now handles modifier key_release events too. Mostly just mash some keys and make sure you get back what you're expecting when testing with the script included in the example. If you have an AltGr key, it would be good to update the table entry for that too, as I can't check that.

@jklymak
Copy link
Member

I've never heard of an AltGr key so I guess if don't have one. I'll try and check tomorrow

Copy link
Member

@dstansbydstansby left a comment

Choose a reason for hiding this comment

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

Works well for me, and fixes issues as advertised. I haven't reviewed the code in_macosx.m, but the doc update looks good.

@dstansby
Copy link
Member

Oh, and I don't think macs have AltGr keys, so leaving that empty is fine.

greglucasand others added4 commitsNovember 30, 2021 07:49
The flagsChanged event handles single presses of modifier keys,so we need to send the corresponding string to our key press handlersfrom within that routine. Modifier + second key is handled withinthe KeyUp/KeyDown routines already, so leave those alone.
Handle the key-press and release events on macosx.
This removes half of the else-if blocks and combines thekeypress/keyrelease for each key into a single statement with negationfor whether it was a press or release being handled.
- This updates the if conditional to use XOR logic instead of the  two negated and/or conditions.- It also handles the shift modifier when combined with other modifier  keys, as before (ctrl + shift + cmd) would have not printed out the  middle shift.
@greglucas
Copy link
ContributorAuthor

I am going to self-merge this based on the 3 approvals and to avoid future rebases. It is a bugfix, but it has been broken for a while, so I don't think we should worry about backporting this.

@greglucasgreglucas merged commit4569a70 intomatplotlib:mainNov 30, 2021
@greglucasgreglucas deleted the macosx-keypress branchNovember 30, 2021 23:43
@QuLogicQuLogic mentioned this pull requestSep 9, 2022
2 tasks
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@anntzeranntzeranntzer approved these changes

@jklymakjklymakjklymak approved these changes

@dstansbydstansbydstansby approved these changes

Assignees
No one assigned
Labels
GUI: MacOSXOS: ApplePR: bugfixPull requests that fix identified bugs
Projects
None yet
Milestone
v3.6.0
Development

Successfully merging this pull request may close these issues.

Modifier key press events not recognized on MacOSX backend MacOS: Key modifiers deprecated
4 participants
@greglucas@anntzer@jklymak@dstansby

[8]ページ先頭

©2009-2025 Movatter.jp