Reputation: 1228
I need to send mouse click to my NSView
. Documentation says about NSEvent +mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:
function. But I can't found info how to exactly use this. Which values of timestamp
, eventNumber
and clickNumber
should be used? Also no type for click event. There are types for mouse down and mouse up events. Whether I send both them in one moment? Or I must do some delay? Currently I try to use such code.
// self.label is NSTextField
[self.label setAllowsEditingTextAttributes: YES];
[self.label setSelectable: YES];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Google"];
[str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0, str.length)];
[self.label setAttributedStringValue:str];
NSEvent *mouseEvent;
mouseEvent = [NSEvent mouseEventWithType:NSLeftMouseDown
location:NSMakePoint(0., 0.)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
eventNumber:0
clickCount:1
pressure:1.];
[self.label mouseDown:mouseEvent];
mouseEvent = [NSEvent mouseEventWithType:NSLeftMouseUp
location:NSMakePoint(0., 0.)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
eventNumber:0
clickCount:1
pressure:1.];
[self.label mouseUp:mouseEvent];
Seems it works. But after such click when I move mouse pointer to label mouse pointer is not showing as hand (to open url). My initital problem url is not showing in NSTextField
as url. It looks as regular text. After click on any place in label (not link) text becames highlighted. So I want to do programmatically click after NSWindow
showing to apply click workaround.
P.S. Actually initial questiong with URL's in NSTextField
solved. I rewrite my code to use NSTextView
instead of NSTextField
. It fully worked as expected but requires more customization. Also IB automatically created NSScrollView
for NSTextView
. Anyway question about correct creating NSEvent
and sending is still actual.
Upvotes: 1
Views: 1258
Reputation: 1966
I use the following code to create a fake event to pop up a context menu relative to a WKWebView position I send via Javascript. Anyway it should be universal to be used for other purposes as well:
NSPoint clickPoint = NSMakePoint(x, y);
NSEvent * evt = [NSEvent mouseEventWithType:NSEventTypeLeftMouseUp
location:[view convertPoint:clickPoint toView:nil]
modifierFlags:0
timestamp:0
windowNumber:view.window.windowNumber
context:nil
eventNumber:0
clickCount:1
pressure:1.];
Upvotes: 1
Reputation: 71
You don’t need to mouse click it. Making it the first responder is enough. But that is fragile. There are a lot topics about that original problem NSTextField with URL-style format
I had the same problem and in the end, I used a NSTextView instead of the NSTextField.
Upvotes: 0
Reputation: 11
To change mouse pointer you can write a customized text field class, and override "resetCursorRects"
you can refer link: Can't change the mouse cursor of a NSTextField
Upvotes: 0