可可keyDown事件未调用 [英] Cocoa keyDown Event doesn't call

查看:67
本文介绍了可可keyDown事件未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

#import <Cocoa/cocoa.h>
#import <AppKit/AppKit.h>

@interface OGLView: NSOpenGLView{}
@end

@implementation OGLView

- (void)keyDown:(NSEvent *)event {
NSLog(@"Hi there");//never called
}

- (BOOL)acceptsFirstResponder{return YES;}

- (BOOL)becomeFirstResponder{return YES;}

- (BOOL)resignFirstResponder{return YES;}

- (BOOL)canBecomeKeyView { return YES; }
@end

int main( int argc, char* args[] ){
NSWindow *win = nil;
NSRect e = [[NSScreen mainScreen] frame];

win = [ [NSWindow alloc]
         initWithContentRect: e
                 styleMask: NSTitledWindowMask
                            |NSClosableWindowMask
                            |NSMiniaturizableWindowMask
                   backing: NSBackingStoreBuffered
                defer: NO ];


view =[[[OGLView alloc] initWithFrame:e]
                       autorelease];


    [win orderFrontRegardless];
    [win setReleasedWhenClosed:YES];
    [win setContentView:view];

    [win setInitialFirstResponder:view];
    [win setNextResponder:view];
    [win makeFirstResponder:view];


    [win setAcceptsMouseMovedEvents:YES];



[view setNeedsDisplay:YES];
[view display];
}

我是否必须创建NSResponder子calss?还是NSCOntroller?如何将该子类连接到OGLView?请帮助。我是Objective-C的新手。我也是在Eclipse上编程(不是Xcode)

Does I must create NSResponder subcalss? Or NSCOntroller? How can I connect this subclasses to my OGLView? Please help.. I'm newbie in Objective-C. Also I'm programming on Eclipse (not Xcode)

推荐答案

您不能将此代码放在中main()。没有应用程序对象,因此没有与窗口服务器的连接。没有事件循环。坦白地说,如代码所示,您的程序将立即退出,因为没有任何事情可以阻止执行脱离 main()的结尾。

You can't put this code in main(). There's no application object, so there's no connection to the window server. There's no event loop. Frankly, as the code stands, your program will exit immediately because there's nothing preventing execution from falling off the end of main().

您的 main()应该调用 NSApplicationMain()。理想情况下,您的应用应与Info.plist文件和MainMenu NIB正确捆绑在一起。加载该NIB将实例化您设计的控制器类的实例,并将其指定为应用程序对象的委托。然后,将类似您的早期启动代码放入应用程序委托的 -applicationDidFinishLaunching:方法中。

Your main() should call NSApplicationMain(). Ideally, your app should be properly bundled with an Info.plist file and a MainMenu NIB. Loading that NIB would instantiate an instance of a controller class of your design and designate it as the application object's delegate. Then, you would put early start-up code like yours in the app delegate's -applicationDidFinishLaunching: method.

如果由于某些原因,您拒绝使用NIB,可以在 main()中调用 [NSApplication sharedApplication] 。实例化您的控制器类,并使用 [NSApp setDelegate:yourObject] 将其直接分配为应用程序对象的委托。然后调用 [NSApp运行] 。同样,应在委托人的方法中进行进一步的初始化。

If for some reason you refuse to use NIBs, you can call [NSApplication sharedApplication] in your main(). Instantiate your controller class and directly assign it as the application object's delegate using [NSApp setDelegate:yourObject]. Then call [NSApp run]. Again, further initialization should be done in the delegate's methods.

这篇关于可可keyDown事件未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆