mouseDown: not firing in WebView subclass
I think this should be quite simple, but I cannot make it work.
I want to detect mouse clicks on a WebView...
I've subclassed WebView,开发者_StackOverflow社区 here is the code
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
@interface ResultsWebView : WebView {
}
@end
and
#import "ResultsWebView.h"
@implementation ResultsWebView
- (void)mouseDown:(NSEvent *)theEvent {
NSLog(@"%@", theEvent);
}
@end
In my xib file, I added a WebView and then changed the class to ResultsWebView.
I've checked in runtime and the object is a ResultsWebView, but the mouseDown event is never called...
What am I missing?
WebUIDelegate comes into rescue.
Supposing that you have a WebView instance in your NSWindowController:
WebView *aWebView;
You can set your controller as UIDelegate, as follow:
[aWebView setUIDelegate:self];
implementing the following method within your controller, you will have a form of control over mouse click events:
- (void)webView:(WebView *)sender mouseDidMoveOverElement:
(NSDictionary *)elementInformation modifierFlags:(NSUInteger)
modifierFlags
{
if ([[NSApp currentEvent] type] == NSLeftMouseUp)
NSLog(@"mouseDown event occurred");
}
as a bonus, playing with the elementInformation dictionary you can get additional informations about the DOM element in which click event occurred.
The problem is that the message isn't being sent to the WebView. Instead it is being sent to the private WebHTMLView inside your WebView, which actually processes the mouse events.
http://trac.webkit.org/browser/trunk/Source/WebKit/mac/WebView/WebHTMLView.mm#L3555
You might just want to look into using javascript in your WebView to send the onmousedown events back to your objective-c class.
Maybe this helps:
[window setAcceptsMouseMovedEvents:YES];
精彩评论