2012-03-26 16 views
9

मैं ओएसएक्स के लिए पाइथन में एक साधारण मैक्रो रिकॉर्डर लिखने की कोशिश कर रहा हूं - कुछ ऐसा जो माउस और महत्वपूर्ण घटनाओं को कैप्चर कर सकता है क्योंकि स्क्रिप्ट पृष्ठभूमि में चलता है और उन्हें फिर से चलाता है। मैं बाद के लिए autopy का उपयोग कर सकता हूं, क्या पूर्व के लिए एक समान सरल पुस्तकालय है?क्या मैं ओएसएक्स में कीबोर्ड और माउस इवेंट कैप्चर करने के लिए पाइथन का उपयोग कर सकता हूं?

+0

कुछ संकुल यहाँ उल्लेख ओएस एक्स समर्थन (जैसे 'keyboard'): https://stackoverflow.com/questions/11918999/key -listeners में अजगर / –

उत्तर

0

ओएसएक्स पर पायथन में ऐसा करने का कोई तरीका प्रतीत नहीं होता है।

1

मुझे पता है कि आप कुंजी इनपुट कैप्चर करने के लिए curses का उपयोग कर सकते हैं, लेकिन मुझे माउस इनपुट के बारे में निश्चित नहीं है। इतना ही नहीं, लेकिन अगर मुझे गलत नहीं लगता है तो इसे 2.7.2 के साथ एसडीडी लाइब्रेरी में शामिल किया गया है।

5

मैं आज इस समस्या के कुछ समाधानों में भाग गया और लगा कि मैं चारों ओर घूमता हूं और यहां साझा करता हूं ताकि अन्य खोज समय बचा सकें।

कीबोर्ड और माउस इनपुट अनुकरण के लिए एक गंधा क्रॉस प्लेटफॉर्म समाधान: http://www.autopy.org/

मैं भी कैसे की स्ट्रोक लोग इन विश्व स्तर पर एक संक्षिप्त लेकिन काम कर (माउंटेन शेर के रूप में) उदाहरण मिल गया। एकमात्र चेतावनी यह है कि आपको Python2.6 का उपयोग करना है क्योंकि 2.7 में ओबीजेसी मॉड्यूल उपलब्ध नहीं हैं।

#!/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() या अन्य विशेषताओं तक पहुंच सकते हैं।

-2

कैल्विन चेंग, धन्यवाद। आपका सुझाव ओएस एक्स 10.8.5 पर काम करता है।

कोड 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) 

एक और समाधान से Key Listeners in python?