NSOpenGLView、NSWindow 和 NSOpenGLView NSResponder - makeFirstResponder 不工作

发布于 2024-12-05 12:15:43 字数 1022 浏览 2 评论 0原文

在下面的代码中,我使用 NSWindowNSOpenGLView 初始化一个 NSViewController [a NSResponder],呈现视图并尝试设置NSViewController 作为 Windows 第一响应者。

它不起作用。我期望能够在下面的 keyUp:keyDown: 方法中命中断点,但什么也没有发生。

我错过了什么吗?

-(void)initwithFrame:(CGRect)frame 
{
    window = [[MyNSWindow alloc] initWithContentRect:frame styleMask:NSClosableWindowMask | NSTitledWindowMask backing:NSBackingStoreBuffered                           defer: YES ];   


    OpenGLView* glView = [[[OpenGLView alloc] initWithFrame:window.frame] autorelease];

    window.contentView = glView;

    [window makeFirstResponder:self];   
    [window makeKeyWindow];     

    [window display];   
}   

-(void)keyDown:(NSEvent*)theEvent
{
    unichar unicodeKey = [ [ theEvent characters ] characterAtIndex:0 ];
    unicodeKey = 0;
}

-(void)keyUp:(NSEvent *)theEvent
{
    unichar unicodeKey = [ [ theEvent characters ] characterAtIndex:0 ];
    unicodeKey = 0;
}

In the code below I am Initialising a NSViewController [a NSResponder], with a NSWindow, a NSOpenGLView, presenting the view and attempting to set the NSViewController as the windows first responder.

It is not working. I was expecting to be able to hit a breakpoint in the keyUp: and keyDown: methods also below, but nothing is happening.

Am I missing something?

-(void)initwithFrame:(CGRect)frame 
{
    window = [[MyNSWindow alloc] initWithContentRect:frame styleMask:NSClosableWindowMask | NSTitledWindowMask backing:NSBackingStoreBuffered                           defer: YES ];   


    OpenGLView* glView = [[[OpenGLView alloc] initWithFrame:window.frame] autorelease];

    window.contentView = glView;

    [window makeFirstResponder:self];   
    [window makeKeyWindow];     

    [window display];   
}   

-(void)keyDown:(NSEvent*)theEvent
{
    unichar unicodeKey = [ [ theEvent characters ] characterAtIndex:0 ];
    unicodeKey = 0;
}

-(void)keyUp:(NSEvent *)theEvent
{
    unichar unicodeKey = [ [ theEvent characters ] characterAtIndex:0 ];
    unicodeKey = 0;
}

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

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

发布评论

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

评论(3

人事已非 2024-12-12 12:15:43

回到这个问题,其实问题出在别的地方。

我一直在使用这个睡眠功能来控制应用程序的帧速率:

void System::Sleep(double seconds)
{
    NSDate* limit       = [NSDate dateWithTimeIntervalSinceNow:seconds];
    NSRunLoop* runLoop  = [NSRunLoop currentRunLoop];

    [runLoop runUntilDate:limit];
}

这样做似乎会完全冻结系统并阻止关键事件。

相反,使用它来安排更新功能:

[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(updateApp:) userInfo:nil repeats:YES];

Returning to this issue, actually the problem is elsewhere.

I had been using this sleep function to control the apps frame rate:

void System::Sleep(double seconds)
{
    NSDate* limit       = [NSDate dateWithTimeIntervalSinceNow:seconds];
    NSRunLoop* runLoop  = [NSRunLoop currentRunLoop];

    [runLoop runUntilDate:limit];
}

Doing this seems to freeze the system completely and block the key events.

Instead use this to schedule the update function:

[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(updateApp:) userInfo:nil repeats:YES];
白芷 2024-12-12 12:15:43

对于参与键视图循环的实例,自定义视图必须从 acceptsFirstResponder 返回 YES

For instances to participate in key-view loops, a custom view must return YES from acceptsFirstResponder.

蓝眼泪 2024-12-12 12:15:43

我也有这个问题。此线程可能会帮助

keyDown 未被调用

我发现是什么阻止了 keyDown 事件被调用。
这是 NSBorderlessWindowMask 掩码,它可以防止窗口
成为关键和主窗口。所以我创建了一个子类
NSWindow 称为 BorderlessWindow:

 @interface BorderlessWindow : NSWindow { }

 @end

 @implementation BorderlessWindow

 - (BOOL)canBecomeKeyWindow {
     return YES; }

 - (BOOL)canBecomeMainWindow {
     return YES; }

 @end

I had this problem also. This thread might help

keyDown not being called

I found out what was preventing the keyDown event from being called.
It was the NSBorderlessWindowMask mask, it prevents the window from
become the key and main window. So I have created a subclass of
NSWindow called BorderlessWindow:

 @interface BorderlessWindow : NSWindow { }

 @end

 @implementation BorderlessWindow

 - (BOOL)canBecomeKeyWindow {
     return YES; }

 - (BOOL)canBecomeMainWindow {
     return YES; }

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