Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

qwaser

macrumors newbie
Original poster
Aug 27, 2012
1
0
I creating NSWindow programmatically and i can't receive any keyboard messages. Instead of this i typing in Xcode editor, but my window is in focus at this time. How i can intercept this events?

Here is my code:
Code:
//// delegate
@interface MyDelegate : NSObject
@end
@implementation MyDelegate
@end

//// view
@interface MyView : NSView
@end

@implementation MyView

- (BOOL)isOpaque { return YES;}
- (BOOL)canBecomeKeyView { return YES;}
- (BOOL)acceptsFirstResponder { return YES;}

- (void)keyDown:(NSEvent *)event
{
    printf("PRESS\n"); // it's ignoring
}

@end

//// main
int main(int argc, const char **argv){
    [NSApplication sharedApplication];

    NSWindow *window = [[NSWindow alloc]
              initWithContentRect:NSMakeRect( 0, 0, 100, 100 )
              styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
              backing:NSBackingStoreBuffered
              defer:NO];
    [window setContentView: [[MyView alloc] init]];
    [window setDelegate: [[MyDelegate alloc] init] ];
    [window setAcceptsMouseMovedEvents:YES];
    [window setLevel: NSFloatingWindowLevel];
    [window makeKeyAndOrderFront: nil];

    [NSApp run];
    return 0;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.