键盘事件 Objective C

发布于 2024-10-16 09:28:47 字数 1110 浏览 3 评论 0原文

我在接收 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-10-23 09:28:47

您必须将 NSView 设置为第一响应者

- (BOOL)acceptsFirstResponder {
返回是;
}

You have to set your NSView to be first responder

- (BOOL)acceptsFirstResponder {
return YES;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文