Mouse messages with unfocused window

Jules, I read in one of your other posts that mouse messages don’t get generated on Mac unless a window has focus. That makes sense, because I can’t get the mouse to disappear when it’s over an unfocused app window (which has its cursor set to MouseCursor::NoCursor). But since the app itself is still the foreground process, could there be another way to fake it somehow? Just wondering if you have any suggestions.

To elaborate a bit more, my app allows for a sort of “presentation-mode” view, where you can have fullscreen output on a secondary monitor, used mainly for video playback, while you are simultaneously editing things on the main monitor. If the user accidentally moves the mouse onto the secondary monitor while editing, I don’t want the cursor to be visible, since an audience may be watching.

Hope that makes some kind of sense. Thanks for any advice.

Internally, I already “fake” it, which is probably what you don’t want to happen in your case! In most OSX apps when your mouse leaves the active window, it remains in whatever shape it had, which is why you often see the cursor getting stuck in strange irrelevant shapes as you move it around (in normal OSX apps I often find myself clicking on the menu bar with e.g. a text selection cursor, it’s a really badly designed bit of the OS!) So I made sure that when it leaves a juce window, it always changes back to a normal cursor. What I didn’t want to do was to make it change to reflect the inactive window that it’s over, because that would be completely non-standard behaviour.

Not sure what to suggest, but thinking about it, perhaps my hack should actually be a bit smarter: when the mouse leaves a window, it should change back to the arrow unless it was hidden, in which case it remains hidden?

I’m not sure if that will solve the issue.

The main thing for me is, as long as my app is the foreground process, I want the cursor to change (if necessary) over every app window, even those that are unfocused.

Might this help?

[NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMoved handler:^(NSEvent *mouseMovedEvent) { //do things with mouseMovedEvent }];

Getting the mouse-moves isn’t a problem - I deliberately chose to not make it update the cursor over other windows, for the reasons I explained above: it’s just not how things are done in OSX.

I see. Gotcha.

I’m not sure that making your hack smarter would apply to my case though, because I want the mouse cursor to become hidden when it moves over an unfocused window (as long as the app is the foreground process).

When you say you “deliberately chose” to not make it update over other windows, where exactly in the code is that deliberate choice manifested?

If I added my hack, then could you just set the mouse to invisible when it exits your main window?

I don’t think so; I have a couple windows besides the main one, and only one of them should have the mouse be hidden over it, and then only in certain cases. So somehow I have to know if the mouse is over that specific window.

Quick fix: Use Desktop::registerGlobalMouseListener, and use that to hide the cursor when you need to.

Yup! That worked. Thanks. That’s exactly what I needed.

Alternatively, how do you feel about using a timerCallback to periodically (say, every 200ms or so) ask for Desktop::getInstance().getMousePosition()? I don’t really need all the MouseListener callbacks that I’d get with your suggested method, and I’m worried they might incur extra CPU usage.

[quote=“MusicMan3001”]Yup! That worked. Thanks. That’s exactly what I needed.

Alternatively, how do you feel about using a timerCallback to periodically (say, every 200ms or so) ask for Desktop::getInstance().getMousePosition()? I don’t really need all the MouseListener callbacks that I’d get with your suggested method, and I’m worried they might incur extra CPU usage.[/quote]

I’d recommend sticking with the global listener approach - the overhead’s really trivial, and it’s a more elegant approach than polling.