NSOpenGLView, NSWindow & NSResponder - makeFirstResponder not working

℡╲_俬逩灬. 提交于 2019-12-23 13:00:17

问题


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;
}

回答1:


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];



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/7475394/nsopenglview-nswindow-nsresponder-makefirstresponder-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!