I am writing a simple Mac application designed to run in the background and perform certain actions whenever the user clicks the mouse button. The app is written in Python using PyObjC. I am using addGlobalMonitorForEventsMatchingMask
to watch for NSLeftMouseDown
events:
我正在编写一个简单的Mac应用程序,旨在在后台运行并在用户单击鼠标按钮时执行某些操作。该应用程序使用PyObjC以Python编写。我正在使用addGlobalMonitorForEventsMatchingMask来监视NSLeftMouseDown事件:
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSLeftMouseDownMask, handler)
This code works perfectly when running in the terminal. However, when I bundle it as a standalone app (using py2app) and then launch it, the app doesn't receive any events at first. (Or at least, if it does, it doesn't run the code in my handler method.) Only when I click on the app in the Dock does it start receiving events, and after that, it continues to receive events even after it returns to the background. But it doesn't receive anything until activated once.
在终端中运行时,此代码非常有效。但是,当我将其捆绑为独立应用程序(使用py2app)然后启动它时,应用程序最初不会收到任何事件。 (或者至少,如果确实如此,它不会在我的处理程序方法中运行代码。)只有当我点击Dock中的应用程序时它才开始接收事件,之后,它继续接收事件,甚至在它之后返回后台。但是直到激活一次它才会收到任何东西。
My question is: How can I get my app to start receiving events as soon as it is launched, without having to be activated first by clicking the Dock icon? Is this some known quirk of NSEvents
, or is there perhaps something wrong with my run loop in PyObjC?
我的问题是:如何让我的应用程序在启动后立即开始接收事件,而无需先通过单击Dock图标来激活它?这是一些已知的NSEvents的怪癖,还是我的PyObjC中的运行循环可能有问题?
Any help or guidance is greatly appreciated!
非常感谢任何帮助或指导!
Edit: Upon further testing, it seems that, in fact, my app spontaneously starts receiving notifications about ten seconds after launch, regardless of whether I activate it. Which is slightly annoying, but fine.
编辑:进一步测试后,事实上,我的应用程序似乎在发布后大约十秒钟内自发地开始接收通知,无论我是否激活它。这有点烦人,但很好。
However, if I run the app with either LSUIElement = true
or LSBackgroundOnly = true
in my Info.plist
(which I ultimately want to do, since this app should only run in the background and never appear in the Dock), I never receive notifications. So I am still stuck.
但是,如果我在我的Info.plist中运行带有LSUIElement = true或LSBackgroundOnly = true的应用程序(我最终想要这样做,因为这个应用程序应该只在后台运行并且永远不会出现在Dock中),我从未收到通知。所以我仍然被困住了。
1 个解决方案
#1
1
As you said "Only when I click on the app in the Dock does it start receiving events" , that means the handler gets registered after you click on the app in the Dock.
正如您所说“只有当我点击Dock中的应用程序才会开始接收事件”时,这意味着在您单击Dock中的应用程序后,处理程序会被注册。
So it depends on at which point in the code you are calling this : NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSLeftMouseDownMask, handler) , that is registering the handler.
所以它取决于你在调用它的代码中的哪一点:NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSLeftMouseDownMask,handler),即注册处理程序。
You should register the handler in appdidfinishlaunching function.
您应该在appdidfinishlaunching函数中注册处理程序。
#1
1
As you said "Only when I click on the app in the Dock does it start receiving events" , that means the handler gets registered after you click on the app in the Dock.
正如您所说“只有当我点击Dock中的应用程序才会开始接收事件”时,这意味着在您单击Dock中的应用程序后,处理程序会被注册。
So it depends on at which point in the code you are calling this : NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSLeftMouseDownMask, handler) , that is registering the handler.
所以它取决于你在调用它的代码中的哪一点:NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSLeftMouseDownMask,handler),即注册处理程序。
You should register the handler in appdidfinishlaunching function.
您应该在appdidfinishlaunching函数中注册处理程序。