Mac WebView not receiving mouse events

Hi! For the last months I’ve been runnig my user interface in the browser using @robotconscience’s ofxLibwebsocket to communicate, it works really well.

Now I want to integrate the user interface into the OF app. Since there is no easy way to embed a browser in a Mac app I created ofxMacWebView. It is based on this @admsyn post.

(I tried Awesomium a while ago, but I believe the framework was 40Mb, also if it goes to the App Store I think Apple makes it mandatory to use WebKit if you want to display web content).

I have no clue about Objective-C, so I’m struggling every time I need to make a simple change…

Right now it is displaying content fine but it still have a couple of big issues:

  • Mouse events don’t work. If you click a link nothing happens. Only after the window is manually resized the last click seems to trigger…

  • Keyboard shortucts like: select all, copy or paste also don’t work.

I’ve created a simple example for you to test: ofxMacWebViewExample. I believe it should work on the latest build but haven’t tested yet since I got some issues updating it.

Any help will be appreciated :smile:

Hey @xavivives, those bugs would imply the mac event loop isn’t getting run, or that the webview isn’t set up as the responder for events. See the addLocalMonitorForEventMatchingMask: and makeFirstResponder: bits of ofxPresent for hints. You’ll need to extend the event mask to get mouse events as well.

Apologies for brevity, hopefully it gives you enough to google though :slight_smile: Let me know if you need more hints.

Thanks @admsyn!

I’ve tried what you suggested

[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^ NSEvent * (NSEvent * event)
{
    [window makeFirstResponder: webView];
    return event;
 }]; 

If I add the previous code I’m unable to type anymore. Before I could, only the shortcuts didn’t work.

My understanding is that addLocalMonitorForEventsMatchingMask:NSKeyDownMask captures all the keyboard events before taking the default action. I’m missing something…

The mouse events had no effect either.

I’ve updated the repo with this changes.

Thanks man!

Hey @xavivives, this isn’t my area of expertise either, BUT I think a lot of the stuff you need may be in @julapy’s excellent ofxCocoaWindow. Check the delegate for some of the mouse event pieces.

You may also be able to use his addon to get a native cocoa window, then inject your web view? Not sure on that, though. Good luck!

Thanks @robotconscience! I took a fast look and I didn’t see anything relevant, I’ll take a closer look later.

Since the last OF supports multiple windows, I was planning to use it. Although the OpenGL stuff still runs underneath. Is it possible to disable it? Or set a rect area? That way we could have the webview and the OpenGL on the same window. I think it can be a very interesting setup for a lot of applications.

Hi @admsyn!

I’ve battled a little bit more with this with no success.
This is pretty much all the relevant code:

void ofxMacWebView::setup() {

window = (NSWindow *)ofGetCocoaWindow();
NSRect f;
f = (NSRect){[window.contentView bounds].origin.x, [window.contentView bounds].origin.y, [window.contentView bounds].size.width, [window.contentView bounds].size.height};

webView = [[WebViewController alloc] initWithFrame:f];

[webView setDrawsBackground:YES];
[webView setWantsLayer:YES];
[webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[webView setCanDrawConcurrently:YES];

[webView setUIDelegate:webView];
[webView setEditingDelegate:webView];

[webView setHostWindow:window];
[window.contentView addSubview:webView];

[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^ NSEvent * (NSEvent * event)
{
    [window makeFirstResponder: window.contentView];
    //[window makeFirstResponder: webView];
 return event;
 }];

}

Do you have any extra suggestion? There is something pretty obvious that I’m missing…

Thanks!

Hi @xavivives, did you ever figure this out? I’m seeing the same thing where (most) mouse events don’t work (mouse position + scroll works, but clicks/drags don’t). Oddly, I’ve tried adding a MapKit view (MKMapView) in the same manner and it works fine for all mouse events. Hm!

Hi @mattfelsen! Nope. I didn’t. I have this on hold for now and just using the web browser. Let me know if you find out more.