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.