我正在尝试在 C++ 中实现类似 NSResponder
的类,但我不明白的是:
所有键盘/鼠标交互都被发送到第一响应者,因此如果 NSTextField 当前是第一响应者,那么同一 View 中的另一个 NSButton
如何知道它已被单击?如果 textfield 不想要鼠标点击,它会将它发送到它的 superview,它的 superview 会将它发送到 View Controller 。因此鼠标单击事件永远不会到达按钮。
这可能是很明显的,但我现在无法理解。
最佳答案
All keyboard/mouse interactions are sent to the first responder
这是不正确的。来自Cocoa Event Handling Guide :
The window object … dispatches most events to the NSView object associated with the user action in an NSResponder message such as mouseDown: or keyDown:. …
The object receiving an event message differs slightly by type of event. For mouse and tablet events, the NSWindow object dispatches the event to the view over which the user pressed the mouse or stylus button. It dispatches most key events to the first responder of the key window.
在特定于这些事件类型的相关部分中重复了鼠标和按键事件路径之间的区别。
按钮“知道”它何时被点击,因为它就是被点击的东西。该窗口使用 -[NSView hitTest:]
方法找出鼠标光标下的内容。
关于c++ - 如果不是第一响应者,为什么我可以单击 NSButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23315019/