2012-03-26 4 views
9

저는 OSX 용 Python에서 간단한 매크로 레코더를 작성하려고합니다. 스크립트가 백그라운드에서 실행될 때 마우스 및 키 이벤트를 캡처하여 재생할 수있는 것입니다. 후자의 경우 autopy을 사용할 수 있습니다. 이전과 비슷한 간단한 라이브러리가 있습니까?Python을 사용하여 OSX에서 키보드 및 마우스 이벤트를 캡처 할 수 있습니까?

+0

일부 패키지는 OS X의 지원이 하나 개 더 솔루션 http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time

#!/usr/bin/python import termios, fcntl, sys, os fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) try: while 1: try: c = sys.stdin.read(1) print "Got character", repr(c) except IOError: pass finally: termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) 

에서

코드 (예를'keyboard') : https://stackoverflow.com/questions/11918999/key -listeners-in-python / –

답변

0

OSX에서 파이썬으로 이것을 수행하는 방법이없는 것 같습니다.

1

키 입력을 캡처하는 데 curses을 사용할 수 있지만 마우스 입력은 확실하지 않습니다. 뿐만 아니라 실수가 아니라면 표준 라이브러리 2.7.2에 포함됩니다.

5

오늘이 문제에 대한 몇 가지 해결책을 생각해 보았습니다. 다른 사람들이 검색 시간을 절약 할 수 있도록 여기 저기 돌아 다니며 공유했습니다.

키보드와 마우스 입력을 시뮬레이션을위한 멋진 크로스 플랫폼 솔루션 : http://www.autopy.org/

은 또한 키 스트로크를 기록 전역하는 방법에 대한 짧지 만 (산 사자 현재) 작업 예를 발견했다. 유일한주의 사항은 Python2.6을 사용해야한다는 것입니다. 2.7은 objc 모듈을 사용할 수있는 것 같지 않습니다.

#!/usr/bin/python2.6 

"""PyObjC keylogger for Python 
by ljos https://github.com/ljos 
""" 

from Cocoa import * 
import time 
from Foundation import * 
from PyObjCTools import AppHelper 

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

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

def main(): 
    app = NSApplication.sharedApplication() 
    delegate = AppDelegate.alloc().init() 
    NSApp().setDelegate_(delegate) 
    AppHelper.runEventLoop() 

if __name__ == '__main__': 
    main() 

마우스 입력의 경우, 단순히 여기에 해당 목록에서 해당 마스크 NSKeyDownMask 교체 : http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html#//apple_ref/occ/clm/NSEvent/addGlobalMonitorForEventsMatchingMask:handler : 마우스의 움직임을 추적

예를 들어

, NSMouseMovedMask 작품. 거기에서 event.locationInWindow() 또는 다른 속성에 액세스 할 수 있습니다.