解决方案是设置一个光标矩形,用透明光标覆盖整个窗口,而不是使用 CGDisplayHideCursor,因为它具有神秘的不可读隐藏计数。这真的很强大 - 当鼠标在窗口内时它可靠地隐藏光标并在所有其他时间显示它。
我最终通过查看 Simple DirectMedia Layer (SDL) 2 源代码发现了这一点 - 这是从那里提取的一个最小工作示例。
在你的 NSView 子类实现中覆盖 resetCursorRects:
static NSCursor* invisibleCursor()
{
static NSCursor *invisibleCursor = NULL;
if (!invisibleCursor) {
/* RAW 16x16 transparent GIF */
static unsigned char cursorBytes[] = {
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
};
NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
length:sizeof(cursorBytes)
freeWhenDone:NO];
NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
hotSpot:NSZeroPoint];
}
return invisibleCursor;
}
- (void)resetCursorRects
{
[super resetCursorRects];
[self addCursorRect:[self bounds] cursor:invisibleCursor()];
}