Something like pyHook on OS X

I really work with pyHook , but I also want to write my own program for OS X. If someone knows such a module ... I searched the Internet for a while, but nothing really relevant.

-> The idea is to record keystrokes outside of a python application. My application is a collector of community statistics, so it would be great to have statistics from OS X.

Thanks in advance;)

Edit : PyHook: record keystrokes and other things outside the python application. http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial http://pyhook.sourceforge.net/doc_1.5.0/ http://sourceforge.net/apps/mediawiki/pyhook/index .php? title = Main_Page

+4
source share
2 answers

As far as I know, there is no Python library for this, so you are going to run your own APIs. The good news is that PyObjC (which comes with built-in Python on recent OS releases) often makes this easy.

There are two main options. For any of these actions, your application must have a Cocoa / CoreFoundation working environment (as on Windows, many things require you to have a “Windows GUI executable” rather than a “command line executable”), which I won’t explain how this to do. (Find a good tutorial for creating graphical Python applications if you don’t know how this happens because this is the easiest way.)

An easy option is the Cocoa Global Event Monitoring API. However, it has some serious limitations. You only get events that go to another application, which means that media keys, global hot keys and keys that are ignored for any reason will not be displayed. In addition, you need to "trust availability." (The easiest way to do this is to ask the user to enable it globally in the "Universal Access" panel in the "System Preferences".)

The tough option is the Quartz event tap API. This is much more flexible, and it only requires the appropriate rights (which, depending on the parameters you use, may include trust in accessibility and / or running as root), and it is much more powerful, but it requires much more work to get started, and you can ruin your system if you make a mistake (for example, you ate all the keystrokes and mouse events so that they never hit the OS, and you cannot reboot except the power button).

For links to all relevant functions, see https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsevent_Class/Reference/Reference.html (for NSEvent) and https: // developer .apple.com / library / mac / # documentation / Carbon / Reference / QuartzEventServicesRef / Reference / reference.html (for quartz events). A bit of googling should show a lot of code examples in Objective-C (for NSEvent) or C (for CGEventTap), but little or nothing in Python, so I will show a few small snippets that illustrate how you port samples for Python:

import Cocoa def evthandler(event): pass # this is where you do stuff; see NSEvent documentation for event observer = Cocoa.NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDown, evthandler) # when you're done Cocoa.NSEvent.removeMonitor_(observer) import Quartz def evthandler(proxy, type, event, refcon): pass # Here where you do your stuff; see CGEventTapCallback return event source = Quartz.CGEventSourceCreate(Quartz.kCGEventSourceStateHIDSystemState) tap = Quartz.CGEventTapCreate(Quartz.kCGSessionEventTap, Quartz.kCGHeadInsertEventTap, Quartz.kCGEventTapOptionListenOnly, (Quartz.CGEventMaskBit(Quartz.kCGEventKeyDown) | Quartz.CGEventMaskBit(Quartz.kCGEventKeyUp)), handler, refcon) 

Another option, at about the same level as Quartz events, are Carbon events (starting with InstallEventHandler). However, Carbon is deprecated, and it’s also harder to get from Python, so if you don’t have any specific reason for this, don’t do it.

There are several other ways to get to one point — for example, use DYLD_INSERT_LIBRARIES or SIMBL to insert some kind of code into each application, but I cannot think of anything that can be done in pure Python.

+9
source

Possible quick alternative, maybe this

https://github.com/gurgeh/selfspy He claims to work with both Mac and windows. It is based on pyhook on a part of the window.

Good luck.

0
source

All Articles