4

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;
}
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
Seamus
  • 1,107
  • 10
  • 22

3 Answers3

3

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];
Seamus
  • 1,107
  • 10
  • 22
2

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
Community
  • 1
  • 1
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
2

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

Rudi Rüssel
  • 439
  • 4
  • 13
  • Since making this post I have tried adding the following to the view class: -(BOOL)acceptsFirstResponder { return YES; } Did not affect the behaviour. – Seamus Sep 20 '11 at 19:30
  • @BROK3NS0UL Try something like - (void)keyDown:(NSEvent*)theEvent { NSString *characters = [theEvent characters]; NSLog(@"Key: ", characters); } – Rudi Rüssel Sep 21 '11 at 03:50