Cocoa 生成左键事件时如何生成右键事件?
我想在同一个对象上生成右键单击事件,在该对象上生成左键单击事件 我需要生成的对象是“上下文菜单”。正如我们所知,上下文菜单仅在右键单击时打开。 所以我想通过在左键单击时生成右键单击事件,在左键单击时打开上下文菜单Cocoa 生成左键事件时如何生成右键事件?,cocoa,Cocoa,我想在同一个对象上生成右键单击事件,在该对象上生成左键单击事件 我需要生成的对象是“上下文菜单”。正如我们所知,上下文菜单仅在右键单击时打开。 所以我想通过在左键单击时生成右键单击事件,在左键单击时打开上下文菜单 - (void)mouseDown:(NSEvent *)theEvent { [self setState:NSOnState]; [self highlight:YES]; if ([self menu]) { [NSMenu popU
- (void)mouseDown:(NSEvent *)theEvent
{
[self setState:NSOnState];
[self highlight:YES];
if ([self menu])
{
[NSMenu popUpContextMenu:[self menu] withEvent:theEvent forView:self];
}
[self setState:NSOffState];
[self highlight:NO];
}
任何人都可以帮我做同样的事情吗?我在interface builder中为我的按钮指定了一个菜单,然后将其子类化为NSButton,以便在鼠标按下(左键单击)时显示菜单
斯威夫特3/iOS 10版本,适用于斯科特的回答:
override func mouseDown(with event: NSEvent)
{
super.mouseDown(with: event)
if let menu = self.menu
{
let locationInView = self.convert(event.locationInWindow, from: nil)
menu.popUp(positioning: nil, at: locationInView, in: self)
}
}
现在请你回答我的问题。我真的同意这个。