如何在wxPython应用程序中使用Mac OS X NSEvents? [英] How to use Mac OS X NSEvents within wxPython application?

查看:122
本文介绍了如何在wxPython应用程序中使用Mac OS X NSEvents?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个必须对Mac OS X上的系统级按键作出反应的应用程序.

I am writing an application that has to react to system wide keypresses on Mac OS X.

因此,我找到了一些可以正常工作的关键记录器示例,因为当我的应用程序以wxPython编写时,所有示例均基于NSSharedApplication()和PyObjC AppHelper.runEventLoop().

So I found some key logger examples that should work and hit a wall, because all examples are based on NSSharedApplication() and PyObjC AppHelper.runEventLoop() while my application is written in wxPython.

在这里,我发布了 https://github.com/ljos 中最简单的示例的修改版本 我认为它应该起作用.但事实并非如此.

Here I post a modification of the simplest example from https://github.com/ljos that I thought it should work. But it does not.

from AppKit import *
import wx

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)

def handler(event):
    print (u"%@", event)

app = wx.App()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
app.MainLoop()

很明显,MainLoop()不能捕获委托的NSEvents.

It is obvious that the MainLoop() doesn't catch the delegated NSEvents.

在app = wx.App()之后,正确返回了NSApp().那为什么不起作用呢?我该如何运作?

After app = wx.App() the NSApp() is returned correctly. So why doesn't this work? How do I make it work?

推荐答案

没有人回答,我以不同的角度四处寻找.

As nobody answered I went searching around with different angle in view.

因此,我发现Quartz模块可用于获取键盘和鼠标事件.不需要自定义循环,因此wx.App()和wx.App.MainLoop()不会造成麻烦.

So I discovered that Quartz module can be used to get to keyboard and mouse events. No custom loop needed, therefore wx.App() and wx.App.MainLoop() aren't getting in the way.

我还找到了一个名为pynput的好程序包,它可以为我完成此工作,因此节省了很多时间. Quartz非常复杂,函数和常量都有很多混乱的名称.但这做得很好.

I also found a nice package named pynput that does just that for me, thus sparing me plenty of time. Quartz is pretty complicated, a lot of scrambled names for functions and constants. But it does a good job.

这篇关于如何在wxPython应用程序中使用Mac OS X NSEvents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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