鼠标按下行为 NSControl 和 NSView 的第一响应者 [英] First responder on mouse down behavior NSControl and NSView

查看:28
本文介绍了鼠标按下行为 NSControl 和 NSView 的第一响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件.如果它继承自 NSView,当我点击它时它会自动成为第一响应者.如果它继承自 NSControl,则不会.这种行为差异仍然存在,即使我覆盖了 mouseDown(with:) 并且不调用 super.

I have a custom control. If it inherits from NSView, it automatically becomes the first responder when I click on it. If it inherits from NSControl, it does not. This difference in behavior persists, even if I override mouseDown(with:) and don't call super.

代码:

class MyControl: NSView {
  override var canBecomeKeyView: Bool      { return true }
  override var acceptsFirstResponder: Bool { return true }
  override func drawFocusRingMask()        { bounds.fill() }
  override var focusRingMaskBounds: NSRect { return bounds }

  override func draw(_ dirtyRect: NSRect) {
    NSColor.white.set()
    bounds.fill()
  }
}

如您所见,我覆盖了 acceptsFirstResponder 以及与关键视图和响应者相关的其他方法和属性.我还检查了 refusesFirstResponder 属性.设置为 false.

As you can see, I override acceptsFirstResponder among other methods and properties that are key view and responder related. I have also checked the refusesFirstResponder property. It is set to false.

  1. 这种行为差异的原因是什么?
  2. 是否有我可以重写以影响它的方法或属性?
  3. 假设我想要视图在单击时成为第一响应者并且视图继承自 NSControl 的行为,在开始时调用 window!.makeFirstResponder(self)我的鼠标按下事件处理程序是一个很好的解决方案还是有更好的解决方案?
  1. What is the reason for this difference in behavior?
  2. Is there a method or property that I can override to influence it?
  3. Say I want the behavior where the view becomes the first responder when clicked and the view inherits from NSControl, is calling window!.makeFirstResponder(self) at the beginning of my mouse-down event handler a good solution or is there a better one?

推荐答案

要覆盖的属性是 needsPanelToBecomeKey.

一个布尔值,指示视图在处理键盘输入和导航之前是否需要其面板成为关键窗口.

A Boolean value indicating whether the view needs its panel to become the key window before it can handle keyboard input and navigation.

该属性的默认值为 false.子类可以覆盖此属性并使用它们的实现来确定视图是否需要其面板成为关键窗口,以便它可以处理键盘输入和导航.这样的子类也应该覆盖 acceptsFirstResponder 以返回 true.

The default value of this property is false. Subclasses can override this property and use their implementation to determine if the view requires its panel to become the key window so that it can handle keyboard input and navigation. Such a subclass should also override acceptsFirstResponder to return true.

此属性也用于键盘导航.它确定鼠标单击是否应将焦点放在视图上——即,使其成为第一响应者).某些视图(例如,文本字段)希望在您单击它们时接收键盘焦点.其他视图(例如,按钮)只有在您使用 Tab 键时才会获得焦点.您不希望仅仅因为您点击了一个复选框就将焦点从正在进行编辑的文本字段转移.

This property is also used in keyboard navigation. It determines if a mouse click should give focus to a view—that is, make it the first responder). Some views (for example, text fields) want to receive the keyboard focus when you click in them. Other views (for example, buttons) receive focus only when you tab to them. You wouldn't want focus to shift from a textfield that has editing in progress simply because you clicked on a check box.

NSView 返回 trueNSControl 返回 false.

这篇关于鼠标按下行为 NSControl 和 NSView 的第一响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆