OS X上的Swift。如何處理全局鼠標事件?

[英]Swift on OS X. How to handle global mouse events?


I am new to Swift and Xcode and I have next problem:

我是Swift和Xcode的新手,我有下一個問題:

I have simple Cocoa Swift Application with one counter (Label). enter image description here

我有一個簡單的Cocoa Swift應用程序和一個計數器(Label)。

How to handle all mouse clicks in my Mac (in all applications) and display it in my Label?

如何處理我的Mac(在所有應用程序中)中的所有鼠標點擊並將其顯示在我的標簽中?

I use Xcode 7.3.1.

我使用Xcode 7.3.1。

UPDATE 1. What I have alredy found it's Monitoring Events and addGlobalMonitorForEventsMatchingMask:handler: function in Cocoa, but I'm not sure that I'm on the right way.

更新1.我有什么alredy發現它是監控事件和addGlobalMonitorForEventsMatchingMask:處理程序:Cocoa中的函數,但我不確定我是否正確。

1 个解决方案

#1


5  

You are correct about using addGlobalMonitorForEventsMatchingMask:handler:

你正確使用addGlobalMonitorForEventsMatchingMask:handler:

A simple example might look something like this:

一個簡單的例子看起來像這樣:

AppDelegate.swift

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet var textLabel : NSTextField!
    var eventHandler : GlobalEventMonitor?
    var gecount : Int = 0

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        eventHandler = GlobalEventMonitor(mask: .LeftMouseDownMask, handler: { (mouseEvent: NSEvent?) in
        self.gecount += 1
        self.textLabel.stringValue = "global event monitor: \(self.gecount)"
    })
    eventHandler?.start()
  }
}

GlobalEventMonitor.swift

public class GlobalEventMonitor {

    private var monitor: AnyObject?
    private let mask: NSEventMask
    private let handler: NSEvent? -> ()

    public init(mask: NSEventMask, handler: NSEvent? -> ()) {
        self.mask = mask
        self.handler = handler
    }

    deinit {
        stop()
    }

    public func start() {
        monitor = NSEvent.addGlobalMonitorForEventsMatchingMask(mask, handler: handler)
    }

    public func stop() {
        if monitor != nil {
            NSEvent.removeMonitor(monitor!)
            monitor = nil
        }
    }
}

Events are delivered asynchronously to your app and you can only observe the event; you cannot modify or otherwise prevent the event from being delivered to its original target application. Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted).

事件以異步方式發送到您的應用程序,您只能觀察事件;您無法修改或以其他方式阻止將事件傳遞到其原始目標應用程序。如果啟用了輔助功能,或者您的應用程序受信任以進行輔助功能訪問,則只能監視與密鑰相關的事件(請參閱AXIsProcessTrusted)。

Note that your handler will not be called for events that are sent to your own application.

請注意,不會為發送到您自己的應用程序的事件調用您的處理程序。

In order to capture events within your app your can either use the addLocalMonitorForEventsMatchingMask:handler: or the NSClickGestureRecognizer object.

為了捕獲應用程序中的事件,您可以使用addLocalMonitorForEventsMatchingMask:handler:或NSClickGestureRecognizer對象。

If you wanted to combine the global event monitor with the gesture recognizer object it's simply a matter of implementing both objects into your class.

如果您想將全局事件監視器與手勢識別器對象組合在一起,那么只需將兩個對象實現到您的類中即可。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/21/b88e911840c620b088dfa5ac3431c1fc.html



 
  © 2014-2022 ITdaan.com 联系我们: