gpt4 book ai didi

objective-c - Cocoa - 当按下按键时 NSCursor 重置为默认光标

转载 作者:行者123 更新时间:2023-12-03 17:20:45 26 4
gpt4 key购买 nike

我正在开发一个应用程序,该应用程序有一个带有全尺寸 NSOpenGLView 的窗口。我使用 [view addCursorRect] 和 [cursor set] 来显示自定义光标,但是当我按键盘上的任意键时,光标会重置为默认箭头。我还尝试过重写resetCursorRects并在按下按键时调用invalidateCursorRects,这会导致光标闪烁。

当我单击 View 中的任意位置时,光标会切换回自定义光标,因此我认为键盘按下会以某种方式使我的 View 失去焦点。有什么办法可以防止按下按键时 View 失去焦点吗?

最佳答案

您需要记住您的光标设置+当您需要重置调用时,例如refreshCursor方法。实现示例:

- (void)refreshCursor
{
NSCursor *cursor = nil;
if ([self graphic]) {
cursor = [graphic hoverCursorForPoint:[self mousePosition]];
} else if ([self group]){
cursor = [NSCursor openHandCursor];
}
[self setHoverCursor:cursor];
[self refresh];
}

- (void)updateTrackingAreas
{
[super updateTrackingAreas];
[self removeTrackingArea:[self trackingArea]];
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self visibleRect]
options:NSTrackingActiveAlways|NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved
owner:self userInfo:nil];
[self setTrackingArea:area];
[self addTrackingArea:[self trackingArea]];
}

- (void)setCursorRects
{
[self discardCursorRects];
if ([self hoverCursor]) {
[self addCursorRect:[self visibleRect] cursor:[self hoverCursor]];
}
}

- (void)setHoverCursor:(NSCursor *)hoverCursor {
if (_hoverCursor != hoverCursor) {
_hoverCursor = hoverCursor;
[self setCursorRects];
}
}

- (void)resetCursorRects {
[super resetCursorRects];
[self setCursorRects];
}

- (void)mouseExited:(NSEvent *)event
{
[[NSCursor currentSystemCursor] set];
}

- (void)mouseEntered:(NSEvent *)event
{
}

- (void)mouseMoved:(NSEvent *)event
{
[self refreshCursor];
}

- (void)keyUp:(NSEvent *)event
{
[self refreshCursor];
}

//allow key events
- (BOOL)acceptsFirstResponder
{
return YES;
}

关于objective-c - Cocoa - 当按下按键时 NSCursor 重置为默认光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com