sliding NSSlider blocks NSEvent monitoring for Keys and wise versa, How to catch them congruent

Viewed 67

I have an NSView with several NSButton and one NSSlider in it.
I am watching keyCode Events with [NSEvent addLocalMonitorForEventsMatchingMask:...handler:^{}] and change the buttons appearance with .highlighted=YES && NO.

I chose this handler method because I have to act on quite unusual event.keyCode combinations that can not be expressed via [NSButton setKeyEquivalent:(NSString * _Nonnull)] and/or [NSButton setKeyEquivalentModifierMask:(NSEventModifierFlags)].

All works quite well and smooth (all keys can be pressed at once, singular or in any combination) unless NSSlider comes into play for which i use traditional event handling with a predefined Action on a Target.

Symptom: The Slider slides nice until I hit any Key and slides again when lifting Keys. Or the other way around, when I hit a Key and want to slide, the Slider just does not slide at all.

I hope i just ran into some edge case of NSResponder and NSEvent handling in general and can learn something. My goal is to be able to press any key while dragging freely with the slider at the same time.

So how can i achieve congruent Event Handling/Executing between Keycodes and MouseEvents of KeyEquivalent NSButtons and native modifierFlag changing behaviour of NSSliders in one and the same NSView?

1 Answers

There are two problems that clash.

NSSlider have special behaviour when key is down.

And [NSEvent addLocalMonitorForEventsMatchingMask:...handler:^{ }] with a mask set to catch NSEventMaskFlagsChanged will cancel each others functionality out when the flag returns nil into the Responder chain.

The modifierFlags to step NSSlider up and down with its altIncrementValue or other special behaviour in combination with other modifierFlags can not work properly when it never reaches the NSView's action method.

Releasing the key lets the Slider slide again, Keys where useless then. Pressing a key (specially a flag) sets the NSSlider in stepwise increment mode but its mouse events are dumped as they where not catched from LocalMonitor which was focused on KeyCode events only.

So the solution is to explicit check for event.type == NSEventTypeLeftMouseDown and for sure subclassing NSSlider to change this interferences with flags or to implement a different NSControl imposing as slider.

Related