Re: FlagsChanged while NSMenu is displayed
Re: FlagsChanged while NSMenu is displayed
- Subject: Re: FlagsChanged while NSMenu is displayed
- From: John Joyce <email@hidden>
- Date: Sun, 26 Jan 2014 03:02:13 +0900
So, just to follow up.
Timer approach works.
It works quite well. Thanks for recommending it.
In fact, far better than the CGEventTap approach in terms of reliability. CGEventTaps always seem to be a bit flaky.
The NSTimer approach is very responsive and I can constrain it to just the time when the interesting menu is displayed and it’s a lot less code than CGEventTap.
Borrowing from the old Dave DeLong post (I don’t do exactly the same thing of course as that post was about achieving a slightly different behavior.)
Initially, I didn’t go with the timer approach as it seems so different from the normal event stream but menus seem to be that way.
So, I add a timer to my app delegate that only runs when the menu in question matches the menu I am interested in.
In
- (void)menuWillOpen:(NSMenu *)menu
I simply add a timer to the NSEventTrackingRunLoopMode
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSEventTrackingRunLoopMode];
Then I invalidate it if it isValid and not nil in
- (void)menuDidClose:(NSMenu *)menu
I may need to tweak the timing of the timer so it isn’t overkill but is responsive.
The timer calls a method to check the modifierFlags
NSUInteger flags = [NSEvent modifierFlags];
if ((flags == NSControlKeyMask) || flags == (NSControlKeyMask|NSAlphaShiftKeyMask)) {
and handle if the control is down or not.
I will file a documentation bug requesting it be in a tech note or in a doc related to NSEvent or NSMenu as it is not an obvious thing to do, but could certainly be useful for others.
On Jan 25, 2014, at 5:15 AM, Jens Alfke <email@hidden> wrote:
> Have you tried running a timer that periodically checks the state of the modifier keys and adds/removes the menu item?
>
> —Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden