10.5 中的 NSMenu 按下鼠标按钮

发布于 2024-10-08 21:25:26 字数 645 浏览 7 评论 0原文

我正在更新(降级?)我为 10.6+ 编写的应用程序,以便在 10.5+ 中运行。我正在努力捕获 -(void)menuWillOpen:(NSMenu *); 选择器中当前按下的鼠标按钮。

对于 10.6+,我正在利用 [NSEvent PressMouseButtons] 它允许我在事件流之外获取按下的按钮。但是,这在 10.5+ 中不存在(看来我需要调用 [theEvent buttonNumber]

如何捕获按下的鼠标按钮(右或左):

  1. 在我的 NSMenu 委托内部
  2. 最好在内部-(void)menuWillOpen:(NSMenu *)menu 选择器
  3. 在一个同时适用于 10.5+ 和 10.6+ 版本的庄园中,

我非常感谢您的帮助,并且知道 StackOverflow 将帮助新的 Objective-C 程序员!

谢谢, 达斯汀

I’m updating (downdating?) an application I’ve written for 10.6+ to work in 10.5+. I’m struggling with capturing the currently pressed mouse button in the -(void)menuWillOpen:(NSMenu *); selector.

For 10.6+ I’m taking advantage of the [NSEvent pressedMouseButtons] which allows me to get the pressed button outside the event stream. However, this doesn’t exist in 10.5+ (it appears I need to call [theEvent buttonNumber].

How do I capture the pressed mouse button (right or left):

  1. Inside of my NSMenu delegate
  2. Preferably inside the -(void)menuWillOpen:(NSMenu *)menu selector
  3. In a manor that works in both 10.5+ and 10.6+

I really appreciate the help and know StackOverflow will help a new Objective-C programmer out!

Thanks,
Dustin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-10-15 21:25:26

最后,我能够通过调用获取当前的鼠标按钮(感谢 Nick Paulson 的帮助):

[[[NSApplicationsharedApplication]currentEvent]buttonNumber]

正如ughoavgfhw指出了一种获取相同内容的更短方法事件是:

[[NSApp currentEvent] buttonNumber]

In the end I was able get the current mouse button by calling (thanks to Nick Paulson for the help):

[[[NSApplication sharedApplication] currentEvent] buttonNumber]

As ughoavgfhw pointed out a shorter way of fetching the same event is though:

[[NSApp currentEvent] buttonNumber]

假扮的天使 2024-10-15 21:25:26

另一种方法是创建一个捕获鼠标按下事件的 CGEventTap。
然后,您只需记住最后一个事件类型并在您的 NSMenu 委托方法之一中进行检查。
下面是一些示例代码:

CGEventRef MouseClickedEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon)
{
    CGPoint location = CGEventGetLocation(event);   
    switch (type) 
    {
        case kCGEventLeftMouseDown:
        {
            NSLog(@"Left Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location)));
            break;
        }           
        case kCGEventRightMouseDown:
        {
            NSLog(@"Right Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location)));
            break;
        }   
        case kCGEventOtherMouseDown:
        {
            NSLog(@"Other Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location)));
            break;          
        }
        default:
            break;
    }
    return event;
}

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
    CGEventMask eventMask = CGEventMaskBit(kCGEventLeftMouseDown)|CGEventMaskBit(kCGEventRightMouseDown)|CGEventMaskBit(kCGEventOtherMouseDown);
    CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, MouseClickedEventCallback, NULL);
    CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(eventTap, true);
}

Quartz 事件服务文档指出:

事件点击接收按键向上和按键向下
如果出现以下情况之一,则发生事件
条件成立:

  • 当前进程正在运行
    根用户。
  • 获取辅助功能
    设备已启用。在 Mac OS X v10.4 中,
    您可以使用启用此功能
    系统偏好设置、通用访问
    面板,键盘视图。

我仔细检查了一下,看来您确实不必启用辅助设备来接收鼠标事件。

但如果 [[[NSApplication sharedApplication] currentEvent] buttonNumber] 满足您的要求,我肯定会同意。它的级别更高,因此代码更少。

Another way would be to create a CGEventTap that captures mouse down events.
You then just have to remember the last event type and check that in one of your NSMenu delegate methods.
Here is some sample code:

CGEventRef MouseClickedEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon)
{
    CGPoint location = CGEventGetLocation(event);   
    switch (type) 
    {
        case kCGEventLeftMouseDown:
        {
            NSLog(@"Left Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location)));
            break;
        }           
        case kCGEventRightMouseDown:
        {
            NSLog(@"Right Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location)));
            break;
        }   
        case kCGEventOtherMouseDown:
        {
            NSLog(@"Other Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location)));
            break;          
        }
        default:
            break;
    }
    return event;
}

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
    CGEventMask eventMask = CGEventMaskBit(kCGEventLeftMouseDown)|CGEventMaskBit(kCGEventRightMouseDown)|CGEventMaskBit(kCGEventOtherMouseDown);
    CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, MouseClickedEventCallback, NULL);
    CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(eventTap, true);
}

The Quartz Event Services docs state that:

Event taps receive key up and key down
events if one of the following
conditions is true:

  • The current process is running as the
    root user.
  • Access for assistive
    devices is enabled. In Mac OS X v10.4,
    you can enable this feature using
    System Preferences, Universal Access
    panel, Keyboard view.

I double checked that and it seems that you really don't have to enable assistive devices to receive mouse events.

But if [[[NSApplication sharedApplication] currentEvent] buttonNumber] does what you want, I'd definitely go with that. It's higher level and therefore less code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文