如何在Mac OS X中获取当前类型的鼠标光标? [英] How to get current type of mouse cursor in Mac OS X?

查看:1782
本文介绍了如何在Mac OS X中获取当前类型的鼠标光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在屏幕上获取当前类型的鼠标光标? (不仅在我的应用程序窗口,全局。)或者至少可以检测默认光标是否当前显示?

How can I get the current type of mouse cursor on screen? (Not only on my app window, globally.) Or is it at least possible to detect whether the default cursor is currently displayed? Either Carbon or Cocoa is OK – or even other working APIs, preferably the official ones.

这是我尝试过的:

NSCursor *sysCursor = [NSCursor currentSystemCursor];

if (sysCursor == nil) {
    NSLog(@"nil");
}

if ([sysCursor isEqual: [NSCursor arrowCursor]] || 
    [sysCursor isEqual: [NSCursor contextualMenuCursor]] || 
    [sysCursor isEqual: [NSCursor closedHandCursor]] || 
    [sysCursor isEqual: [NSCursor crosshairCursor]] || 
    [sysCursor isEqual: [NSCursor disappearingItemCursor]] || 
    [sysCursor isEqual: [NSCursor dragCopyCursor]] || 
    [sysCursor isEqual: [NSCursor dragLinkCursor]] || 
    [sysCursor isEqual: [NSCursor IBeamCursor]] || 
    [sysCursor isEqual: [NSCursor openHandCursor]] || 
    [sysCursor isEqual: [NSCursor operationNotAllowedCursor]] || 
    [sysCursor isEqual: [NSCursor pointingHandCursor]] || 
    [sysCursor isEqual: [NSCursor resizeDownCursor]] || 
    [sysCursor isEqual: [NSCursor resizeLeftCursor]] || 
    [sysCursor isEqual: [NSCursor resizeLeftRightCursor]] || 
    [sysCursor isEqual: [NSCursor resizeRightCursor]] || 
    [sysCursor isEqual: [NSCursor resizeUpCursor]] || 
    [sysCursor isEqual: [NSCursor resizeUpDownCursor]] || 
    [sysCursor isEqual: [NSCursor IBeamCursorForVerticalLayout]]
    ) {
    NSLog(@"equal");
} else {
    NSLog(@"not");
}

游标不是 nil ,但同时它不等于任何其他。它甚至不等于自身:

The cursor is not nil, but at the same time it’s not equal to any of the others. It’s not even equal to itself:

NSLog(@"%i", [[NSCursor currentSystemCursor]
    isEqual:[NSCursor currentSystemCursor]]); // 0

想法?这是一个 LSUIElement 类型的应用程序,如果这很重要。

Ideas? This is a LSUIElement-type app, if that matters.

推荐答案

这是一个黑客,但它看起来像可以使用 hotSpot 属性来区分至少一些游标:

This is quite a hack, but it looks like it’s possible to tell at least some cursors apart using the hotSpot property:

NSLog(@"%@", NSStringFromPoint([[NSCursor currentSystemCursor] hotSpot]));

这会返回 {5,5} 默认指针光标。我不知道在某些情况下(如更高的DPI或其他),这个值是否改变默认光标。我已经结束了 NSCursor 这个类别:

This returns {5, 5} for the default pointer cursor. I have no idea if this value changes for the default cursor under some circumstances (like higher DPI or whatever else). I have ended up with this category on NSCursor:

- (BOOL) isDefaultCursor
{
    NSPoint defaultCursorHotspot = [[NSCursor arrowCursor] hotSpot];
    return NSEqualPoints(defaultCursorHotspot, [self hotSpot]);
}

除此之外,还有一个 _flags.cursorType 实例变量,但这是受保护的。而且,正如你已经提到的,当前的系统游标不必是 -isEqual:本身。

Other than that, there’s a _flags.cursorType instance variable, but that’s protected. And, as you already mentioned, the current system cursor does not have to be even -isEqual: with itself.

这篇关于如何在Mac OS X中获取当前类型的鼠标光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆