OSX 中的 KeyDown 操作?

发布于 2024-10-14 18:35:02 字数 107 浏览 6 评论 0原文

我已经为我的 keydown 操作完成了所有代码,但我不知道如何处理我访问的每个站点似乎都会略过的第一响应者。谁能告诉我如何设置它来识别可可 Objectivec 中的按键操作?

谢谢

I have the code all done for my keydown actions, but i dont know what to do with the first responder that every site i go to seems to skim over. Can anyone tell me how to set it up to recognise keydown actions in cocoa objectivec?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

乖乖 2024-10-21 18:35:02

首先,keyDown: 是一个事件消息,而不是一个操作消息。请注意,它的参数是 NSEvent,而不是某种 UI 对象(例如 NSControl 或 NSMenuItem)。

操作消息沿着响应者链向下传递,在这种情况下,“第一响应者”并不特殊。每个响应者都会将其不知道如何处理的任何操作消息传递给下一个响应者。这就是“响应者链”。第一响应者就是位于响应者链头部的任何响应者,即第一个。您只需要处于该链中,位于任何不知道如何响应传递给它的操作的后面。

但由于这是一条事件消息,所以情况有所不同。您需要成为关键视图这是第一响应者

这就是全部内容。您需要在视图中响应 keyDown: 消息(以及可能相关的消息),并且该视图需要是接收该消息的第一个响应者。

NSResponder 类参考Cocoa 事件处理指南 将告诉您更多信息。

First, keyDown: is an event message, not an action message. Note that its argument is an NSEvent, not a UI object of some sort (such as an NSControl or NSMenuItem).

Action messages go down the responder chain, in which case the “first responder” is not special. Each responder will hand any action message it doesn't know how to handle off to its next responder. This is the “responder chain”. The first responder is simply whatever responder is at the head of the responder chain—i.e., is first. You would simply need to be in that chain, behind anything that doesn't know how to respond to the action being passed down it.

But since this is an event message, things are different. You need to be the key view, which is the first responder.

And that's all there is to it. You need to respond to the keyDown: message (and possibly related ones) in a view, and that view needs to be the first responder to receive the message.

The NSResponder class reference and Cocoa Event-Handling Guide will tell you more.

極樂鬼 2024-10-21 18:35:02

这是我所做的并且效果很好。 (适用于 macOS Sierra 的 Swift 3)

override func viewDidLoad() {
    keyIsDown = false // variable defined in the NSViewController

    NSEvent.addLocalMonitorForEvents(matching: .keyUp) { (aEvent) -> NSEvent? in
        self.keyUp(with: aEvent)
        return aEvent
    }

    NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (aEvent) -> NSEvent? in
        self.keyDown(with: aEvent)
        return aEvent
    }
}

现在我还覆盖了这些:

override var acceptsFirstResponder: Bool { return true }
override func becomeFirstResponder() -> Bool { return true }
override func resignFirstResponder() -> Bool { return true }

以及这些:

override func keyUp(with event: NSEvent) {
    keyIsDown = false
    if event.keyCode == 1 {
        print("s key released")
    }
}

override func keyDown(with event: NSEvent) {
    if keyIsDown == true {
        return
    }
    keyIsDown = true
    // Whatever you'd like to do (check to see which key released, etc.)
}

这应该可以帮助您入门。

Here is what I've done and it works well. (Swift 3 for macOS Sierra)

override func viewDidLoad() {
    keyIsDown = false // variable defined in the NSViewController

    NSEvent.addLocalMonitorForEvents(matching: .keyUp) { (aEvent) -> NSEvent? in
        self.keyUp(with: aEvent)
        return aEvent
    }

    NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (aEvent) -> NSEvent? in
        self.keyDown(with: aEvent)
        return aEvent
    }
}

Now I also override these:

override var acceptsFirstResponder: Bool { return true }
override func becomeFirstResponder() -> Bool { return true }
override func resignFirstResponder() -> Bool { return true }

And these:

override func keyUp(with event: NSEvent) {
    keyIsDown = false
    if event.keyCode == 1 {
        print("s key released")
    }
}

override func keyDown(with event: NSEvent) {
    if keyIsDown == true {
        return
    }
    keyIsDown = true
    // Whatever you'd like to do (check to see which key released, etc.)
}

That should get you started.

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