Reputation: 274
I've seen a lot of questions, but I couldn't find anything what helped me. I've looked at many Apple developer pages, but I find those to be a bit unclear.
I want to make applications in Objective-C++ without Xcode or any other IDE that does all the work for me. My IDE is Atom, and I compile with g++. I have the following class to create a window:
//Window.mm
#ifndef WINDOW_H
#define WINDOW_H
#import "Cocoa/Cocoa.h"
class Window
{
private: NSWindow* window;
public: Window(const char* title, int x, int y, int w, int h, NSColor* bg = [NSColor colorWithCalibratedRed:0.3f green:0.3f blue:0.3f alpha:1.0f])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
NSRect frame = NSMakeRect(x, y, w, h);
NSUInteger windowStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:windowStyle];
this->window = [[[NSWindow alloc] initWithContentRect:rect styleMask:windowStyle backing:NSBackingStoreBuffered defer:NO] autorelease];
[this->window makeKeyAndOrderFront: this->window];
[this->window setBackgroundColor: bg];
[this->window setTitle: [NSString stringWithUTF8String:title]];
[this->window orderFrontRegardless];
[pool drain];
[NSApp run];
}
};
#endif
From what I did understand is that I need to do something with an NSView, but I'm not sure what I'm supposed to do. How will I be able to get key input from my window?
Upvotes: 1
Views: 682
Reputation: 359
You need to subclass NSWindow in order to receive key input events, for example:
KWCustomWindow.h:
#import <Cocoa/Cocoa.h>
@interface KWCustomWindow : NSWindow
@end
KWCustomWindow.m
#import "KWCustomWindow.h"
@implementation KWCustomWindow
- (void)keyDown:(NSEvent *)event
{
NSLog(@"Key Down");
}
@end
Upvotes: 1