您可以尝试使用本地事件监视器拦截所有鼠标左键事件。在此块中,您将确定点击是否发生在您的收藏视图上。如果确实如此,请创建一个新事件来模仿您截获的事件,但如果它不存在,则添加命令键掩码。然后,在块的末尾返回您的事件,而不是您拦截的事件。您的集合视图将表现得好像用户按下了命令键,即使他们没有!
我在一个非常简单的演示应用程序中快速完成了这一点,它看起来是一种很有前途的方法——尽管我希望你在此过程中必须协商一些问题。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskFromType(NSLeftMouseDown)
handler:^NSEvent *(NSEvent *originalEvent) {
// Did this left down event occur on your collection view?
// If it did add in the command key
NSEvent *newEvent =
[NSEvent
mouseEventWithType: NSLeftMouseDown
location: originalEvent.locationInWindow
modifierFlags: NSCommandKeyMask // I'm assuming it's not already present
timestamp: originalEvent.timestamp
windowNumber: originalEvent.windowNumber
context: originalEvent.context
eventNumber: originalEvent.eventNumber
clickCount: originalEvent.clickCount
pressure:0];
return newEvent; // or originalEvent if it's nothing to do with your collection view
}];
}
编辑(由问题作者):
该解决方案非常基于原始答案,因此该答案值得称赞(随意编辑)
您还可以通过继承 NSCollectionView 类并覆盖 mousedown 来拦截鼠标事件,如下所示:
@implementation MyCollectionView
-(void) mouseDown:(NSEvent *)originalEvent {
NSEvent *mouseEventWithCmd =
[NSEvent
mouseEventWithType: originalEvent.type
location: originalEvent.locationInWindow
modifierFlags: NSCommandKeyMask
timestamp: originalEvent.timestamp
windowNumber: originalEvent.windowNumber
context: originalEvent.context
eventNumber: originalEvent.eventNumber
clickCount: originalEvent.clickCount
pressure: originalEvent.pressure];
[super mouseDown: mouseEventWithCmd];
}
@end