键盘事件 Objective C
我在接收 NSView 子类中的键盘事件时遇到问题。
我可以很好地处理鼠标事件,但我的 keyDown 和 keyUp 方法从未被调用。据我了解,根据文档,两种类型的事件都遵循相同的层次结构,但情况似乎并非如此。
这是第一响应者问题吗?某处某个领域吸引了焦点?我尝试过覆盖它,但没有运气。
任何见解将不胜感激。
如果您想查看..这是在自定义 NSView 类中:
#pragma mark -
#pragma mark I/O Events
-(void)keyDown:(NSEvent *)theEvent {
NSLog(@"Sup brah!");
}
-(void)keyUp:(NSEvent *)theEvent {
NSLog(@"HERE");
}
// This function works great:
-(void)mouseDown:(NSEvent *)theEvent {
NSNumber *yeah = [[NSNumber alloc] initWithBool:YES];
NSNumber *nah = [[NSNumber alloc] initWithBool:NO];
NSString *asf = [[NSString alloc] initWithFormat:@"%@", [qcView valueForOutputKey:@"Food_Out"]];
if ([asf isEqualToString:@"1"]) {
[qcView setValue:nah forInputKey:@"Food_In"];
best_food_x_loc = convertToQCX([[qcView valueForOutputKey:@"Food_1_X"] floatValue]);
best_food_y_loc = convertToQCY([[qcView valueForOutputKey:@"Food_1_Y"] floatValue]);
NSLog(@"X:%f, Y:%f",best_food_x_loc, best_food_y_loc);
} else {
[qcView setValue:yeah forInputKey:@"Food_In"];
}
}
I'm having trouble receiving keyboard events within a subclass of NSView.
I can handle mouse events fine, but my keyDown and keyUp methods are never called. It was my understanding per the documentation that both types of events follow the same hierarchy, however this is seemingly not the case.
Is this a first responder issue? Some field somewhere grabbing the focus? I've tried overriding that but no luck.
Any insights would be greatly appreciated.
If you'd like to see.. this is within a custom NSView class:
#pragma mark -
#pragma mark I/O Events
-(void)keyDown:(NSEvent *)theEvent {
NSLog(@"Sup brah!");
}
-(void)keyUp:(NSEvent *)theEvent {
NSLog(@"HERE");
}
// This function works great:
-(void)mouseDown:(NSEvent *)theEvent {
NSNumber *yeah = [[NSNumber alloc] initWithBool:YES];
NSNumber *nah = [[NSNumber alloc] initWithBool:NO];
NSString *asf = [[NSString alloc] initWithFormat:@"%@", [qcView valueForOutputKey:@"Food_Out"]];
if ([asf isEqualToString:@"1"]) {
[qcView setValue:nah forInputKey:@"Food_In"];
best_food_x_loc = convertToQCX([[qcView valueForOutputKey:@"Food_1_X"] floatValue]);
best_food_y_loc = convertToQCY([[qcView valueForOutputKey:@"Food_1_Y"] floatValue]);
NSLog(@"X:%f, Y:%f",best_food_x_loc, best_food_y_loc);
} else {
[qcView setValue:yeah forInputKey:@"Food_In"];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将 NSView 设置为第一响应者
- (BOOL)acceptsFirstResponder {
返回是;
}
You have to set your NSView to be first responder
- (BOOL)acceptsFirstResponder {
return YES;
}