Simulate mouse actions


I’m writing a Max external using an embedded Juce-rendered GUI . To do this, I am rendering to juce::Graphics and grabbing bitmap data to paste in to Max’s graphics context, as described in this thread - it’s a little hacky, but it works and I don’t see any other option.

That’s all working fine. The trouble I’m now faced with is passing back the mouse events from Max so that I can just rely on Juce to resolve the actioned Component and call the relevant listener. The Max API gives me a bunch of mouse event methods with which I was hoping to just call the relevant handlers in Juce as if the event had occurred on my Juce-rendered GUI rather than on my Max bitmap-copy of the GUI - I’m just not sure of the best way to do this. My solution needs to remain platform independent.

Any help would be much appreciated.

If you’ve got the ComponentPeer for the window that you want to forward the mouse events to then you can use ComponentPeer::handleMouseEvent().

The Unity plug-in wrapper does something similar so you might want to check that out for some ideas - specifically the mouse handler callbacks here.

Thanks for that. I’d looked at ComponentPeer but my component doesn’t have one as I don’t addToDesktop(). I tried hacking about to manually createNewPeer(), but it wasn’t quite that simple. I followed the code through the debugger to figure out why coordinates were not resolving to components and ended up hacking about a bit more until I figured it was all getting too messy and there must be a better way.

The component should still have a peer as it’ll be in a window that’s on the desktop. You can get the peer by doing something like:

if (auto* peer = yourComponent.getPeer())
   // forward your mouse events here...

I’m trying to get this working. I have one app that I want to send mouse and keyboard events to another app that is running in the background (no window open). Is this still possible?

		Time theTime = Time::getCurrentTime();
		int theFlags = 0;
		theFlags |= ModifierKeys::Flags::leftButtonModifier;
		ModifierKeys modEvent(theFlags);
		peer->handleMouseEvent(MouseInputSource::InputSourceType::mouse, Point<float>(x, y), modEvent, 1.0f, 1.0f, theTime.toMilliseconds());

It was quite a while ago now, but I did eventually get there. As far as I remember, all I did was manually create instances of juce::MouseEvent and pass them to the relevant mouse method of the top-most juce::Component (depending on the mouse action being performed).
The difficulty was that the Max mouse methods don’t map directly to JUCE mouse methods (in that they don’t provide all of the information you need to populate a juce::MouseEvent). So, I seem to remember I had to write a class that tracked mouse events and was updated every time a Max mouse method was called so that it could provide all of the information I needed to populate a juce::MouseEvent.

Thanks. But I still wonder why I can’t get handleMouseEvent to work at all. I’m checking the FakeMouseMoveGenerator code I found inside JUCE files but I still can’t get it to work. :frowning:

Cheers, WilliamK

Here’s my solution so far. :slight_smile: