4

cancelOperation: is not being called in my bare-bones NSView subclass when I press Esc.

I checked and the Esc key is received on keyDown. Also, other action messages (such as moveLeft) are being called.

The view is part of a Window shown like this:

[self.window addChildWindow:wc.window ordered:NSWindowAbove];
[wc.window makeKeyAndOrderFront:self];

What am I doing wrong?

hpique
  • 119,096
  • 131
  • 338
  • 476

3 Answers3

1

In my case, I have an app with a couple of NSWindows. I had to call [self.window makeFirstResponder:self] in my NSView subclass to have the view respond to cancelOperation:.

Marko Hlebar
  • 1,973
  • 17
  • 18
0

My derived NSView had the same problem. It was resolved after implementing acceptsFirstResponder as follows:

- (BOOL)acceptsFirstResponder
{
    return YES;
}
Ad N
  • 7,930
  • 6
  • 36
  • 80
0

Are you implementing it as cancelOperation or cancelOperation:? There's a big difference. The method signature should be:

- (void)cancelOperation:(id)sender

This works for me with a vanilla NSView.

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • 2
    The problem is probably that you're implementing `keyDown:` but then not calling `interpretKeyEvents:` inside your implementation. You either need to not implement `keyDown:` or make sure you call `[self interpretKeyEvents:[NSArray arrayWithObject:theEvent]]` inside your implementation. – Rob Keniger Feb 26 '12 at 01:13
  • No, I'm not implementing keyDown:. I just tested that Esc was being received, then removed it. – hpique Feb 26 '12 at 10:50