Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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, 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
878 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...