一千萬個為什麽

搜索

退出全屏模式後如何接收NSView的鍵盤事件?

我將子類化為NSView並在應用程序完成啟動時啟動全屏模式。該視圖在應用程序委托中以屬性 fooView 的形式提供。

// AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification*)notification {
  [[self window] makeKeyAndOrderFront:self];
  [[self fooView] enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
}

FooView 本身實現以下功能。

// FooView.m
- (void)keyDown:(NSEvent*)event {
  NSLog(@"%@ %@ - %@", self.className, NSStringFromSelector(_cmd), event);
  [self interpretKeyEvents:[NSArray arrayWithObject:event]];
}
- (void)cancelOperation:(id)sender {
  NSLog(@"%@ %@ - %@", self.className, NSStringFromSelector(_cmd), sender);
  [self exitFullScreenModeWithOptions:nil];
}

離開全屏模式後,視圖不再接收鍵盤事件。為什麽?

Edit:
It seems to have something to do with how I exit the fullscreen mode. When I click into the view (not the window) the keyDown: and cancelOperation: do respond in the following.

最佳答案

問題是包含視圖的窗口確實收到了任何鍵盤事件。在離開全屏模式後,需要使窗口成為第一響應者。

- (void)cancelOperation:(id)sender {
  NSLog(@"%@ %@ - %@", self.className, NSStringFromSelector(_cmd), sender);
  [self exitFullScreenModeWithOptions:nil];
  [self.window makeFirstResponder:self];
}

轉載註明原文: 退出全屏模式後如何接收NSView的鍵盤事件?