开发者

NSOpenGLView, NSWindow & NSResponder - makeFirstResponder not working

开发者 https://www.devze.com 2023-04-05 18:22 出处:网络
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.

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


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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消