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

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

objective c - 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, 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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...