NSStatusItem右键菜单
我正在开发一个具有左键和右键单击功能的状态栏应用程序。我已经按照其他帖子的提示开始了这项工作,但我不确定如何在右键单击时显示菜单。
我使用子类 NSView 作为 NSStatusItem 的自定义视图,并通过右键单击和左键单击执行不同的功能:
- (void)mouseDown:(NSEvent *)theEvent{
[super mouseDown:theEvent];
if ([theEvent modifierFlags] & NSCommandKeyMask){
[self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
}else{
[self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO];
}
}
- (void)rightMouseDown:(NSEvent *)theEvent{
[super rightMouseDown:theEvent];
[self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
}
如何在右键单击时显示菜单,就像标准 NSStatusItem 在左键单击时显示菜单一样?
I'm working on a status bar app that has a left and right click. I've got the start of this working by following the tips from other posts but I'm not sure how to go about showing a menu on right click.
I use a subclassed NSView as the custom view of my NSStatusItem and have the right and left clicks executing different functions:
- (void)mouseDown:(NSEvent *)theEvent{
[super mouseDown:theEvent];
if ([theEvent modifierFlags] & NSCommandKeyMask){
[self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
}else{
[self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO];
}
}
- (void)rightMouseDown:(NSEvent *)theEvent{
[super rightMouseDown:theEvent];
[self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
}
How can I show a menu on right click, the same way the standard NSStatusItem does on left click?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSStatusItem popUpStatusItemMenu:
成功了。我通过右键单击操作调用它并传递我想要显示的菜单,它正在显示它!这不是我所期望的这个功能,但它正在工作。这是我的代码的重要部分:
这是自定义视图的实现:
NSStatusItem popUpStatusItemMenu:
did the trick. I am calling it from my right click action and passing in the menu I want to show and it's showing it! This is not what I would have expected this function to do, but it's working.Here's the important parts of what my code looks like:
Here is the implementation for the custom view:
一种选择是假装鼠标左键按下:
One option is to just fake the left mouse down:
当您在视图中需要标题时添加一些小东西
Added little something for when you need title in your view