开发者

Getting global scroll events with PyObjC

How can I get global scroll events with PyObjC? Can it be done with an NSEvent call?

Sam开发者_开发百科ple code would be great...


You were on the right track with NSEvent! As long as you don't want to modify the event, but just observe it, it's as easy as calling addGlobalMonitorForEventsMatchingMask:handler:. Your app will get notified via a callback whenever an event of the type you specify with the mask is posted to another app.* The handler argument is a block, but need cause you no worry, because block arguments are almost easier to deal with in PyObjC than in straight Obj-C: you can pass any callable object (function, method, class, etc.) and the bridge will handle the rest. This is all you need to do:

def callback(event):
    NSLog(u"%s" % event)

NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSScrollWheelMask, callback)

In the callback, you get a copy of the actual event, that you can query for things like its deltaX or whatever you like.

Note, that, like so many other interesting methods in AppKit, this one is new in 10.6, and isn't in the metadata file for older (read: Apple-supplied) versions of PyObjC. This means that if you try to build an app using the default install of the bridge, it will fail. You'll have to use a newer version.


*If you want to get events posted to your app, you must use addLocalMonitorForEventsMatchingMask:handler:. Unfortunately, one event monitor cannot get both events for your app and for others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜