NSOpenGLView、NSWindow 和 NSOpenGLView NSResponder - makeFirstResponder 不工作
在下面的代码中,我使用 NSWindow
和 NSOpenGLView
初始化一个 NSViewController
[a NSResponder],呈现视图并尝试设置NSViewController
作为 Windows 第一响应者。
它不起作用。我期望能够在下面的 keyUp:
和 keyDown:
方法中命中断点,但什么也没有发生。
我错过了什么吗?
-(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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回到这个问题,其实问题出在别的地方。
我一直在使用这个睡眠功能来控制应用程序的帧速率:
这样做似乎会完全冻结系统并阻止关键事件。
相反,使用它来安排更新功能:
Returning to this issue, actually the problem is elsewhere.
I had been using this sleep function to control the apps frame rate:
Doing this seems to freeze the system completely and block the key events.
Instead use this to schedule the update function:
对于参与键视图循环的实例,自定义视图必须从 acceptsFirstResponder 返回 YES。
For instances to participate in key-view loops, a custom view must return YES from acceptsFirstResponder.
我也有这个问题。此线程可能会帮助
keyDown 未被调用
I had this problem also. This thread might help
keyDown not being called