def applicationDidFinishLaunching_(self, notification): downMask = NSKeyDownMask upMask = NSKeyUpMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( downMask, downHandler) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( upMask, upHandler)
def applicationDidFinishLaunching_(self, notification): NSLog("Application did finish launching...") # Register preferance defaults for user-facing preferences prefDictionary = {} prefDictionary[u"screenshots"] = True prefDictionary[u'imageSize'] = 720 # in px prefDictionary[u"imageTimeMax"] = 60 # in s prefDictionary[u"imageTimeMin"] = 100 # in ms prefDictionary[u"experienceTime"] = 1800 # in s prefDictionary[u"experienceLoop"] = True prefDictionary[u"recording"] = True NSUserDefaultsController.sharedUserDefaultsController().setInitialValues_(prefDictionary) mask = (NSKeyDownMask | NSKeyUpMask | NSLeftMouseDownMask | NSLeftMouseUpMask | NSRightMouseDownMask | NSRightMouseUpMask | NSMouseMovedMask | NSScrollWheelMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler) self.createStatusMenu() # self.createStatusButton() NSNotificationCenter.defaultCenter().postNotificationName_object_('checkLoops',self) NSNotificationCenter.defaultCenter().postNotificationName_object_('noteRecordingState',self)
def start_click_listener(self): mask = (NSLeftMouseDownMask | NSRightMouseDownMask ) # | NSLeftMouseUpMask | NSRightMouseUpMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, self.click_handler)
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyDownMask | NSLeftMouseDownMask | NSLeftMouseUpMask | NSRightMouseDownMask | NSRightMouseUpMask | NSMouseMovedMask | NSScrollWheelMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler)
def applicationDidFinishLaunching_(self, notification): #print "Sucessfully launched Obj-C application" mask = (NSKeyDownMask | NSLeftMouseDownMask | NSRightMouseDownMask | NSMouseMovedMask | NSScrollWheelMask) #| NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler)
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyDownMask | NSLeftMouseDownMask | NSLeftMouseUpMask | NSRightMouseDownMask | NSRightMouseUpMask | NSMouseMovedMask | NSScrollWheelMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, sc.handler)
def click_handler(self, event): recording = preferences.getValueForPreference('recording') event_screenshots = preferences.getValueForPreference('eventScreenshots') if event_screenshots: self.sniffer.activity_tracker.take_screenshot() if recording: # check if the clipboard has updated self.sniffer.clr.get_clipboard_contents() # get data ready to write loc = NSEvent.mouseLocation() scr = NSScreen.screens() xmin = 0 ymin = 0 for s in scr: if s.frame().origin.x < xmin: xmin = s.frame().origin.x if s.frame().origin.y < ymin: ymin = s.frame().origin.y x = int(loc.x) - xmin y = int(loc.y) - ymin #get click type click_type = "Unknown" if event.type() == NSLeftMouseDown: click_type = "Left" elif event.type() == NSRightMouseDown: click_type = "Right" # write JSON object to clicklog file text = '{"time": '+ str(cfg.NOW()) + ' , "button": "' + click_type + '", "location": [' + str(x) + ',' + str(y) + ']}' utils_cocoa.write_to_file(text, cfg.CLICKLOG)
def handler(self, event): try: if event.type() == NSLeftMouseDown: self.mouse_button_hook(1, True) # elif event.type() == NSLeftMouseUp: # self.mouse_button_hook(1, False) elif event.type() == NSRightMouseDown: self.mouse_button_hook(2, True) # elif event.type() == NSRightMouseUp: # self.mouse_button_hook(2, False) elif event.type() == NSKeyDown: self.key_hook(event.keyCode(), None, event.characters(), True, event.isARepeat()) elif event.type() == NSMouseMoved: loc = NSEvent.mouseLocation() self.mouse_move_hook(loc.x, loc.y) if event.type() in [NSLeftMouseDown, NSRightMouseDown, NSMouseMoved]: windowNumber = event.windowNumber() windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID) for window in windowList: if window['kCGWindowNumber'] == windowNumber: self.focus.wm_name = window['kCGWindowName'] self.fucus.app_name = window['kCGWindowOwnerName'] break except KeyboardInterrupt: AppHelper.stopEventLoop()
def move_handler(self, event): recording = preferences.getValueForPreference('recording') event_screenshots = preferences.getValueForPreference( 'eventScreenshots') if event_screenshots: self.sniffer.activity_tracker.take_screenshot() if recording: if event.type() == NSMouseMoved: loc = NSEvent.mouseLocation() # get all the image size information scr = NSScreen.screens() xmin = 0 ymin = 0 for s in scr: if s.frame().origin.x < xmin: xmin = s.frame().origin.x if s.frame().origin.y < ymin: ymin = s.frame().origin.y x = int(loc.x) - xmin y = int(loc.y) - ymin # write JSON object to movelog file text = '{"time": ' + str(cfg.NOW()) + ' , "location": [' + str( x) + ',' + str(y) + ']}' utils_cocoa.write_to_file(text, cfg.MOVELOG)
def handler(self, event): try: if event.type() == NSLeftMouseDown: self.mouse_button_hook(1, True) # elif event.type() == NSLeftMouseUp: # self.mouse_button_hook(1, False) elif event.type() == NSRightMouseDown: self.mouse_button_hook(2, True) # elif event.type() == NSRightMouseUp: # self.mouse_button_hook(2, False) elif event.type() == NSKeyDown: self.key_hook(event.keyCode(), None, event.characters(), True, event.isARepeat()) elif event.type() == NSMouseMoved: loc = NSEvent.mouseLocation() self.mouse_move_hook(loc.x, loc.y) if event.type() in [ NSLeftMouseDown, NSRightMouseDown, NSMouseMoved ]: windowNumber = event.windowNumber() windowList = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID) for window in windowList: if window['kCGWindowNumber'] == windowNumber: self.focus.wm_name = window['kCGWindowName'] self.fucus.app_name = window['kCGWindowOwnerName'] break except KeyboardInterrupt: AppHelper.stopEventLoop()
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyDownMask | NSKeyUpMask | NSLeftMouseDownMask | NSLeftMouseUpMask | NSRightMouseDownMask | NSRightMouseUpMask | NSMouseMovedMask | NSScrollWheelMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler) # use reference to outer class (Sniffer)'s attributes self.start_current_process = sc.start_current_process self.registerNotifications()
def move_handler(self, event): recording = preferences.getValueForPreference('recording') event_screenshots = preferences.getValueForPreference('eventScreenshots') if event_screenshots: self.sniffer.activity_tracker.take_screenshot() if recording: if event.type() == NSMouseMoved: loc = NSEvent.mouseLocation() # get all the image size information scr = NSScreen.screens() xmin = 0 ymin = 0 for s in scr: if s.frame().origin.x < xmin: xmin = s.frame().origin.x if s.frame().origin.y < ymin: ymin = s.frame().origin.y x = int(loc.x) - xmin y = int(loc.y) - ymin # write JSON object to movelog file text = '{"time": '+ str(cfg.NOW()) + ' , "location": [' + str(x) + ',' + str(y) + ']}' utils_cocoa.write_to_file(text, cfg.MOVELOG)
def tap_event_callback(_tap_proxy, event_type, event_ref, _user_info): if event_type != NX_SYSDEFINED: return event_ref with autorelease_pool(): event = NSEvent.eventWithCGEvent_(event_ref) if event.subtype() != NSEventSubtypeScreenChanged: # value of 8, for some reason. return event_ref # pragma: no cover key_code = (event.data1() & 0xFFFF0000) >> 16 if key_code not in _MEDIA_KEYS: return event_ref # pragma: no cover iTunes = None for bundle_name in 'com.apple.Music', 'com.apple.iTunes': iTunes = SBApplication.applicationWithBundleIdentifier_( bundle_name) if iTunes is not None: break if not iTunes.isRunning(): return event_ref # pragma: no cover key_flags = event.data1() & 0x0000FFFF key_is_pressed = (((key_flags & 0xFF00) >> 8)) == 0xA if key_is_pressed: meth_name = _MEDIA_ACTIONS[key_code] getattr(iTunes, meth_name)()
def show_context(self, options, x, y): y = self.frame().size.height - y menu = DropboxNSMenu.menuWithDropboxMenuDescriptor_(options) if MAC_VERSION >= SNOW_LEOPARD: menu.popUpMenuPositioningItem_atLocation_inView_(None, NSMakePoint(x, y), self) return fake_event = NSEvent.mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure_(NSLeftMouseDown, NSMakePoint(x, y), 0, 0, self.window().windowNumber(), None, 0, 0, 0) NSMenu.popUpContextMenu_withEvent_forView_withFont_(menu, fake_event, self, NSFont.systemFontOfSize_(12.75))
def handler(self, event): try: self.find_window() loc = NSEvent.mouseLocation() if event.type() == NSLeftMouseDown: self.mouse_button_hook(1, loc.x, loc.y) # elif event.type() == NSLeftMouseUp: # self.mouse_button_hook(1, loc.x, loc.y) elif event.type() == NSRightMouseDown: self.mouse_button_hook( 3, loc.x, loc.y, ) # elif event.type() == NSRightMouseUp: # self.mouse_button_hook(2, loc.x, loc.y) elif event.type() == NSScrollWheel: if event.deltaY() > 0: self.mouse_button_hook(4, loc.x, loc.y) elif event.deltaY() < 0: self.mouse_button_hook(5, loc.x, loc.y) if event.deltaX() > 0: self.mouse_button_hook(6, loc.x, loc.y) elif event.deltaX() < 0: self.mouse_button_hook(7, loc.x, loc.y) # if event.deltaZ() > 0: # self.mouse_button_hook(8, loc.x, loc.y) # elif event.deltaZ() < 0: # self.mouse_button_hook(9, loc.x, loc.y) elif event.type() == NSKeyDown: flags = event.modifierFlags() modifiers = [] # OS X api doesn't care it if is left or right if flags & NSControlKeyMask: modifiers.append('Ctrl') if flags & NSAlternateKeyMask: modifiers.append('Alt') if flags & NSCommandKeyMask: modifiers.append('Cmd') if flags & (NSShiftKeyMask | NSAlphaShiftKeyMask): modifiers.append('Shift') character = event.charactersIgnoringModifiers() # these two get a special case because I am unsure of # their unicode value if event.keyCode() is 36: character = "Enter" elif event.keyCode() is 51: character = "Backspace" self.key_hook(event.keyCode(), modifiers, keycodes.get(character, character), event.isARepeat()) elif event.type() == NSMouseMoved: self.mouse_move_hook(loc.x, loc.y) except (Exception, KeyboardInterrupt): AppHelper.stopEventLoop() raise
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyDownMask | NSCommandKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSShiftKeyMask | NSLeftMouseDownMask | NSRightMouseDownMask | NSScrollWheel | NSFlagsChangedMask | NSRightMouseDraggedMask | NSLeftMouseDraggedMask | NSScrollWheelMask | NSEventMaskGesture | NSEventMaskMagnify | NSEventMaskSwipe | NSEventMaskRotate | NSEventMaskSmartMagnify) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def applicationDidFinishLaunching_(self, notification): mask = (NSEventTypeKeyDown | NSKeyDown | NSKeyDownMask | NSKeyUp | NSKeyUpMask | NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSRightMouseDownMask | NSRightMouseUpMask | NSRightMouseDraggedMask | NSMouseMovedMask | NSScrollWheelMask | NSSystemDefined | NSFlagsChangedMask) mask = int('0xFFFFFF', 16) mask = numpy.bitwise_xor(mask, NSKeyUpMask) print(bin(mask)) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def handler(self, event): try: self.find_window() loc = NSEvent.mouseLocation() if event.type() == NSLeftMouseDown: self.mouse_button_hook(1, loc.x, loc.y) # elif event.type() == NSLeftMouseUp: # self.mouse_button_hook(1, loc.x, loc.y) elif event.type() == NSRightMouseDown: self.mouse_button_hook(3, loc.x, loc.y,) # elif event.type() == NSRightMouseUp: # self.mouse_button_hook(2, loc.x, loc.y) elif event.type() == NSScrollWheel: if event.deltaY() > 0: self.mouse_button_hook(4, loc.x, loc.y) elif event.deltaY() < 0: self.mouse_button_hook(5, loc.x, loc.y) if event.deltaX() > 0: self.mouse_button_hook(6, loc.x, loc.y) elif event.deltaX() < 0: self.mouse_button_hook(7, loc.x, loc.y) # if event.deltaZ() > 0: # self.mouse_button_hook(8, loc.x, loc.y) # elif event.deltaZ() < 0: # self.mouse_button_hook(9, loc.x, loc.y) elif event.type() == NSKeyDown: flags = event.modifierFlags() modifiers = [] # OS X api doesn't care it if is left or right if flags & NSControlKeyMask: modifiers.append('Ctrl') if flags & NSAlternateKeyMask: modifiers.append('Alt') if flags & NSCommandKeyMask: modifiers.append('Cmd') if flags & (NSShiftKeyMask | NSAlphaShiftKeyMask): modifiers.append('Shift') character = event.charactersIgnoringModifiers() # these two get a special case because I am unsure of # their unicode value if event.keyCode() is 36: character = "Enter" elif event.keyCode() is 51: character = "Backspace" self.key_hook(event.keyCode(), modifiers, keycodes.get(character, character), event.isARepeat()) elif event.type() == NSMouseMoved: self.mouse_move_hook(loc.x, loc.y) except (Exception, KeyboardInterrupt): AppHelper.stopEventLoop() raise
def takeExperienceScreenshot_(self, notification): try: mouseLoc = NSEvent.mouseLocation() x = str(int(mouseLoc.x)) y = str(int(mouseLoc.y)) folder = os.path.join(cfg.CURRENT_DIR,"screenshots") filename = datetime.now().strftime("%y%m%d-%H%M%S%f") + "_" + x + "_" + y + '-experience' path = os.path.join(folder,""+filename+".jpg") # -i makes the screenshot interactive # -C captures the mouse cursor. # -x removes the screenshot sound if notification.object().takeFullScreenshot: command = "screencapture -x -C '" + path + "'" else: command = "screencapture -i -x -C '" + path + "'" # delete current full-screen screenshot for this experience os.system("rm "+ notification.object().currentScreenshot ) os.system(command) notification.object().currentScreenshot = path if not notification.object().takeFullScreenshot: path = os.path.expanduser(path) experienceImage = NSImage.alloc().initByReferencingFile_(path) width = experienceImage.size().width height = experienceImage.size().height ratio = width / height if( width > 360 or height > 225 ): if (ratio > 1.6): width = 360 height = 360 / ratio else: width = 225 * ratio height = 225 experienceImage.setScalesWhenResized_(True) experienceImage.setSize_((width, height)) notification.object().screenshotDisplay.setImage_(experienceImage) except errno.ENOSPC: NSLog("No space left on storage device. Turning off Selfspy recording.") self.delegate.toggleLogging_(self,self) alert = NSAlert.alloc().init() alert.addButtonWithTitle_("OK") alert.setMessageText_("No space left on storage device. Turning off Selfspy recording.") alert.setAlertStyle_(NSWarningAlertStyle) alert.runModal() except: NSLog("Could not save image")
def applicationDidFinishLaunching_(self, notification): # Note: we only monitor key-down events, not key-release if VERBOSE >= 2: print "-> applicationDidFinishLaunching" mask = NSKeyDownMask evH = NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, osx_handler) if not evH: raise RuntimeError( "The Global Monitor couldn't be added the App.")
def applicationDidFinishLaunching_(self, notification): mask = (NSEventTypeKeyDown | NSKeyDown | NSKeyUp | NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSRightMouseDownMask | NSRightMouseUpMask | NSRightMouseDraggedMask | NSMouseMovedMask | NSScrollWheelMask | NSFlagsChangedMask | NSFunctionKeyMask | NSAlternateKeyMask | NSCommandKeyMask | NSControlKeyMask | NSAlphaShiftKeyMask | NSSystemDefined) mask = int('0xFFFFFF', 16) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, self.handler)
def click_handler(self, event): recording = preferences.getValueForPreference('recording') event_screenshots = preferences.getValueForPreference( 'eventScreenshots') if event_screenshots: self.sniffer.activity_tracker.take_screenshot() if recording: # check if the clipboard has updated self.sniffer.clr.get_clipboard_contents() # get data ready to write loc = NSEvent.mouseLocation() scr = NSScreen.screens() xmin = 0 ymin = 0 for s in scr: if s.frame().origin.x < xmin: xmin = s.frame().origin.x if s.frame().origin.y < ymin: ymin = s.frame().origin.y x = int(loc.x) - xmin y = int(loc.y) - ymin #get click type click_type = "Unknown" if event.type() == NSLeftMouseDown: click_type = "Left" elif event.type() == NSRightMouseDown: click_type = "Right" # write JSON object to clicklog file text = '{"time": ' + str(cfg.NOW( )) + ' , "button": "' + click_type + '", "location": [' + str( x) + ',' + str(y) + ']}' utils_cocoa.write_to_file(text, cfg.CLICKLOG)
def screenshot(self, path, region = None): #https://pythonhosted.org/pyobjc/examples/Quartz/Core%20Graphics/CGRotation/index.html try: # record how long it takes to take screenshot start = time.time() # Set to capture entire screen, including multiple monitors if region is None: region = CG.CGRectInfinite # Create CGImage, composite image of windows in region image = CG.CGWindowListCreateImage( region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID, CG.kCGWindowImageDefault ) scr = NSScreen.screens() xmin = 0 ymin = 0 for s in scr: if s.frame().origin.x < xmin: xmin = s.frame().origin.x if s.frame().origin.y < ymin: ymin = s.frame().origin.y nativeHeight = CGImageGetHeight(image)*1.0 nativeWidth = CGImageGetWidth(image)*1.0 nativeRatio = nativeWidth/nativeHeight prefHeight = NSUserDefaultsController.sharedUserDefaultsController().values().valueForKey_('imageSize') height = int(prefHeight/scr[0].frame().size.height*nativeHeight) width = int(nativeRatio * height) heightScaleFactor = height/nativeHeight widthScaleFactor = width/nativeWidth mouseLoc = NSEvent.mouseLocation() x = int(mouseLoc.x) y = int(mouseLoc.y) w = 16 h = 24 scale_x = int((x-xmin) * widthScaleFactor) scale_y = int((y-h+5-ymin) * heightScaleFactor) scale_w = w*widthScaleFactor scale_h = h*heightScaleFactor #Allocate image data and create context for drawing image imageData = LaunchServices.objc.allocateBuffer(int(4 * width * height)) bitmapContext = Quartz.CGBitmapContextCreate( imageData, # image data we just allocated... width, height, 8, # 8 bits per component 4 * width, # bytes per pixel times number of pixels wide Quartz.CGImageGetColorSpace(image), # use the same colorspace as the original image Quartz.kCGImageAlphaPremultipliedFirst # use premultiplied alpha ) #Draw image on context at new scale rect = CG.CGRectMake(0.0,0.0,width,height) Quartz.CGContextDrawImage(bitmapContext, rect, image) # Add Mouse cursor to the screenshot cursorPath = "../Resources/cursor.png" cursorPathStr = NSString.stringByExpandingTildeInPath(cursorPath) cursorURL = NSURL.fileURLWithPath_(cursorPathStr) # Create a CGImageSource object from 'url'. cursorImageSource = Quartz.CGImageSourceCreateWithURL(cursorURL, None) # Create a CGImage object from the first image in the file. Image # indexes are 0 based. cursorOverlay = Quartz.CGImageSourceCreateImageAtIndex(cursorImageSource, 0, None) Quartz.CGContextDrawImage(bitmapContext, CG.CGRectMake(scale_x, scale_y, scale_w, scale_h), cursorOverlay) #Recreate image from context imageOut = Quartz.CGBitmapContextCreateImage(bitmapContext) #Image properties dictionary dpi = 72 # FIXME: Should query this from somewhere, e.g for retina display properties = { Quartz.kCGImagePropertyDPIWidth: dpi, Quartz.kCGImagePropertyDPIHeight: dpi, Quartz.kCGImageDestinationLossyCompressionQuality: 0.6, } #Convert path to url for saving image pathWithCursor = path[0:-4] + "_" + str(x) + "_" + str(y) + '.jpg' pathStr = NSString.stringByExpandingTildeInPath(pathWithCursor) url = NSURL.fileURLWithPath_(pathStr) #Set image destination (where it will be saved) dest = Quartz.CGImageDestinationCreateWithURL( url, LaunchServices.kUTTypeJPEG, # file type 1, # 1 image in file None ) # Add the image to the destination, with certain properties Quartz.CGImageDestinationAddImage(dest, imageOut, properties) # finalize the CGImageDestination object. Quartz.CGImageDestinationFinalize(dest) #For testing how long it takes to take screenshot stop = time.time() print 'took ' + str(height) + 'px image in ' + str(stop-start)[:5] + ' seconds' except KeyboardInterrupt: print "Keyboard interrupt" AppHelper.stopEventLoop() except errno.ENOSPC: NSLog("No space left on storage device. Turning off Selfspy recording.") self.delegate.toggleLogging_(self) except: NSLog("couldn't save image")
def get_mouse_pos(): return Point(NSEvent.mouseLocation().x, NSEvent.mouseLocation().y)
def start_scroll_listener(self): mask = (NSScrollWheelMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, self.scroll_handler)
def start_scroll_listener(self): mask = (NSScrollWheelMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.scroll_handler)
def handler(self, event): try: activeApps = self.workspace.runningApplications() windowNumber = event.windowNumber() #Have to look into this if it is too slow on move and scroll, #right now the check is done for everything. for app in activeApps: if app.isActive(): options = kCGWindowListOptionOnScreenOnly windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) for window in windowList: if (window['kCGWindowNumber'] == windowNumber or (not event.windowNumber() and window['kCGWindowOwnerName'] == app.localizedName())): geometry = window['kCGWindowBounds'] self.screen_hook(window['kCGWindowOwnerName'], window.get('kCGWindowName', u''), geometry['X'], geometry['Y'], geometry['Width'], geometry['Height']) break break loc = NSEvent.mouseLocation() if event.type() == NSLeftMouseDown: self.mouse_button_hook(1, loc.x, loc.y) # elif event.type() == NSLeftMouseUp: # self.mouse_button_hook(1, loc.x, loc.y) elif event.type() == NSRightMouseDown: self.mouse_button_hook(3, loc.x, loc.y) # elif event.type() == NSRightMouseUp: # self.mouse_button_hook(2, loc.x, loc.y) elif event.type() == NSScrollWheel: if event.deltaY() > 0: self.mouse_button_hook(4, loc.x, loc.y) elif event.deltaY() < 0: self.mouse_button_hook(5, loc.x, loc.y) if event.deltaX() > 0: self.mouse_button_hook(6, loc.x, loc.y) elif event.deltaX() < 0: self.mouse_button_hook(7, loc.x, loc.y) # if event.deltaZ() > 0: # self.mouse_button_hook(8, loc.x, loc.y) # elif event.deltaZ() < 0: # self.mouse_button_hook(9, loc.x, loc.y) elif event.type() == NSKeyDown: flags = event.modifierFlags() modifiers = [] # OS X api doesn't care it if is left or right if flags & NSControlKeyMask: modifiers.append('Ctrl') if flags & NSAlternateKeyMask: modifiers.append('Alt') if flags & NSCommandKeyMask: modifiers.append('Cmd') if flags & (NSShiftKeyMask | NSAlphaShiftKeyMask): modifiers.append('Shift') character = event.charactersIgnoringModifiers() # these two get a special case because I am unsure of # their unicode value if event.keyCode() == 36: character = "Enter" elif event.keyCode() == 51: character = "Backspace" self.key_hook(event.keyCode(), modifiers, keycodes.get(character, character), event.isARepeat()) elif event.type() == NSMouseMoved: self.mouse_move_hook(loc.x, loc.y) except (SystemExit, KeyboardInterrupt): AppHelper.stopEventLoop() return except: AppHelper.stopEventLoop() raise
def applicationDidFinishLaunching_(self, notification): print('flaf flag') mask = NSKeyDownMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def start_move_listener(self): mask = (NSMouseMovedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, self.move_handler)
def handler(self, event): try: check_windows = False event_type = event.type() todo = lambda: None if ( time.time() - self.last_check_windows > 10 and event_type != NSKeyUp ): self.last_check_windows = time.time() check_windows = True loc = NSEvent.mouseLocation() if event_type == NSLeftMouseDown: check_windows = True todo = lambda: self.mouse_button_hook(1, loc.x, loc.y) elif event_type == NSRightMouseDown: check_windows = True todo = lambda: self.mouse_button_hook(3, loc.x, loc.y) elif event_type == NSScrollWheel: if event.deltaY() > 0: todo = lambda: self.mouse_button_hook(4, loc.x, loc.y) elif event.deltaY() < 0: todo = lambda: self.mouse_button_hook(5, loc.x, loc.y) if event.deltaX() > 0: todo = lambda: self.mouse_button_hook(6, loc.x, loc.y) elif event.deltaX() < 0: todo = lambda: self.mouse_button_hook(7, loc.x, loc.y) elif event_type == NSKeyDown: flags = event.modifierFlags() modifiers = [] # OS X api doesn't care it if is left or right if flags & NSControlKeyMask: modifiers.append('Ctrl') if flags & NSAlternateKeyMask: modifiers.append('Alt') if flags & NSCommandKeyMask: modifiers.append('Cmd') if flags & (NSShiftKeyMask | NSAlphaShiftKeyMask): modifiers.append('Shift') character = event.charactersIgnoringModifiers() # these two get a special case because I am unsure of # their unicode value if event.keyCode() == 36: character = "Enter" elif event.keyCode() == 51: character = "Backspace" todo = lambda: self.key_hook(event.keyCode(), modifiers, keycodes.get(character, character), event.isARepeat()) elif event_type == NSMouseMoved: todo = lambda: self.mouse_move_hook(loc.x, loc.y) elif event_type == NSFlagsChanged: # Register leaving this window on next event self.last_check_windows = 0 if check_windows: activeApps = self.workspace.runningApplications() for app in activeApps: if app.isActive(): app_name = app.localizedName() options = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) windowListLayered = [ w for w in windowList if w['kCGWindowLayer'] ] windowList = [ w for w in windowList if not w['kCGWindowLayer'] ] windowList = windowList + windowListLayered for window in windowList: if window['kCGWindowOwnerName'] == app_name: geometry = window['kCGWindowBounds'] self.screen_hook(window['kCGWindowOwnerName'], window.get('kCGWindowName', u''), geometry['X'], geometry['Y'], geometry['Width'], geometry['Height']) break break todo() except (SystemExit, KeyboardInterrupt): AppHelper.stopEventLoop() return except: AppHelper.stopEventLoop() raise
def setupKeyCounterMac(self): mask = NSKeyDownMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, self.macCountKey) NSEvent.addLocalMonitorForEventsMatchingMask_handler_( mask, self.macCountKey)
def handler(self, event): try: check_windows = False event_type = event.type() todo = lambda: None if ( time.time() - self.last_check_windows > FORCE_SCREEN_CHANGE and event_type != NSKeyUp ): self.last_check_windows = time.time() check_windows = True loc = NSEvent.mouseLocation() if event_type == NSLeftMouseDown: check_windows = True todo = lambda: self.mouse_button_hook(1, loc.x, loc.y) elif event_type == NSRightMouseDown: check_windows = True todo = lambda: self.mouse_button_hook(3, loc.x, loc.y) elif event_type == NSScrollWheel: if event.deltaY() > 0: todo = lambda: self.mouse_button_hook(4, loc.x, loc.y) elif event.deltaY() < 0: todo = lambda: self.mouse_button_hook(5, loc.x, loc.y) if event.deltaX() > 0: todo = lambda: self.mouse_button_hook(6, loc.x, loc.y) elif event.deltaX() < 0: todo = lambda: self.mouse_button_hook(7, loc.x, loc.y) elif event_type == NSKeyDown: flags = event.modifierFlags() modifiers = [] # OS X api doesn't care it if is left or right if flags & NSControlKeyMask: modifiers.append('Ctrl') if flags & NSAlternateKeyMask: modifiers.append('Alt') if flags & NSCommandKeyMask: modifiers.append('Cmd') if flags & (NSShiftKeyMask | NSAlphaShiftKeyMask): modifiers.append('Shift') character = event.charactersIgnoringModifiers() # these two get a special case because I am unsure of # their unicode value if event.keyCode() == 36: character = "Enter" elif event.keyCode() == 51: character = "Backspace" todo = lambda: self.key_hook(event.keyCode(), modifiers, keycodes.get(character, character), event.isARepeat()) elif event_type == NSMouseMoved: todo = lambda: self.mouse_move_hook(loc.x, loc.y) elif event_type == NSFlagsChanged: # Register leaving this window after animations are done # approx (1 second) self.last_check_windows = (time.time() - FORCE_SCREEN_CHANGE + WAIT_ANIMATION) check_windows = True if check_windows: activeApps = self.workspace.runningApplications() for app in activeApps: if app.isActive(): app_name = app.localizedName() options = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) windowListLowPrio = [ w for w in windowList if w['kCGWindowLayer'] or not w.get('kCGWindowName', u'') ] windowList = [ w for w in windowList if not w['kCGWindowLayer'] and w.get('kCGWindowName', u'') ] windowList = windowList + windowListLowPrio for window in windowList: if window['kCGWindowOwnerName'] == app_name: geometry = window['kCGWindowBounds'] self.screen_hook(window['kCGWindowOwnerName'], window.get('kCGWindowName', u''), geometry['X'], geometry['Y'], geometry['Width'], geometry['Height']) break break todo() except (SystemExit, KeyboardInterrupt): AppHelper.stopEventLoop() return except: AppHelper.stopEventLoop() raise
def start_click_listener(self): mask = (NSLeftMouseDownMask | NSRightMouseDownMask) # | NSLeftMouseUpMask | NSRightMouseUpMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.click_handler)
def screenshot(self, path, region = None): #https://pythonhosted.org/pyobjc/examples/Quartz/Core%20Graphics/CGRotation/index.html try: # record how long it takes to take screenshot start = time.time() scr = NSScreen.screens() # Trying to capture mouse cursor # Quartz.CGDisplayShowCursor(Quartz.CGMainDisplayID()) # Quartz.CGAssociateMouseAndMouseCursorPosition(True) # Set to capture entire screen, including multiple monitors if region is None: region = CG.CGRectInfinite # Create CGImage, composite image of windows in region image = None image = CG.CGWindowListCreateImage( region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID, CG.kCGWindowImageDefault ) xmin = 0 ymin = 0 for s in scr: if s.frame().origin.x < xmin: xmin = s.frame().origin.x if s.frame().origin.y < ymin: ymin = s.frame().origin.y nativeHeight = CGImageGetHeight(image)*1.0 nativeWidth = CGImageGetWidth(image)*1.0 nativeRatio = nativeWidth/nativeHeight prefHeight = NSUserDefaultsController.sharedUserDefaultsController().values().valueForKey_('imageSize') height = int(prefHeight) #int(prefHeight/scr[0].frame().size.height*nativeHeight) width = int(nativeRatio * height) # Computes the scale factor between the user resolution and the native screen resolution resolutionScaleFactor = scr[0].frame().size.height / nativeHeight # Computes the scale factor between the image size in the preferences and the user resolution prefScaleFactor = height / scr[0].frame().size.height mouseLoc = NSEvent.mouseLocation() x = int(mouseLoc.x) y = int(mouseLoc.y) w = 16 h = 24 scale_x = int((x-xmin) * prefScaleFactor) scale_y = int((y-h+5-ymin) * prefScaleFactor) #int((y-h+5-ymin) * heightScaleFactor) scale_w = w*prefScaleFactor scale_h = h*prefScaleFactor #Allocate image data and create context for drawing image imageData = None imageData = LaunchServices.objc.allocateBuffer(int(100)) imageData = LaunchServices.objc.allocateBuffer(int(4 * width * height)) bitmapContext = None bitmapContext = Quartz.CGBitmapContextCreate( imageData, # image data we just allocated... width, height, 8, # 8 bits per component 4 * width, # bytes per pixel times number of pixels wide Quartz.CGImageGetColorSpace(image), # use the same colorspace as the original image Quartz.kCGImageAlphaPremultipliedFirst # use premultiplied alpha ) #Draw image on context at new scale rect = CG.CGRectMake(0.0,0.0,width,height) Quartz.CGContextDrawImage(bitmapContext, rect, image) # Add Mouse cursor to the screenshot cursorPath = "../Resources/cursor.png" cursorPathStr = NSString.stringByExpandingTildeInPath(cursorPath) cursorURL = NSURL.fileURLWithPath_(cursorPathStr) # Create a CGImageSource object from 'url'. cursorImageSource = None cursorImageSource = Quartz.CGImageSourceCreateWithURL(cursorURL, None) # Create a CGImage object from the first image in the file. Image # indexes are 0 based. cursorOverlay = None cursorOverlay = Quartz.CGImageSourceCreateImageAtIndex(cursorImageSource, 0, None) Quartz.CGContextDrawImage(bitmapContext, CG.CGRectMake(scale_x, scale_y, scale_w, scale_h), cursorOverlay) #Recreate image from context imageOut = Quartz.CGBitmapContextCreateImage(bitmapContext) #Image properties dictionary dpi = 72 # FIXME: Should query this from somewhere, e.g for retina display properties = { Quartz.kCGImagePropertyDPIWidth: dpi, Quartz.kCGImagePropertyDPIHeight: dpi, Quartz.kCGImageDestinationLossyCompressionQuality: 0.6, } # Getting id of current window and application try: activeAppName = self.workspace.activeApplication()['NSApplicationName'] except: activeAppName = "" print "failed NSApplicationName" active_app_id = self.getProcessIDFromName(activeAppName) options = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) for window in windowList: # window_name = str(window.get('kCGWindowName', u'').encode('ascii', 'replace')) window_name = self.getWindowName(window) owner = window['kCGWindowOwnerName'] if (activeAppName == owner and window_name != ''): break active_window_id = self.getWindowIDFromName(window_name) # Done with getting id of current window and application #Convert path to url for saving image pathWithCursor = path[0:-4] + "_" + str(x) + "_" + str(y) if (active_app_id != None and active_app_id != '') : pathWithCursor = pathWithCursor + "_app" + str(active_app_id) if (active_window_id != None and active_window_id != '') : pathWithCursor = pathWithCursor + "_win" + str(active_window_id) pathWithCursor = pathWithCursor + '.jpg' # print pathWithCursor pathStr = NSString.stringByExpandingTildeInPath(pathWithCursor) url = NSURL.fileURLWithPath_(pathStr) #Set image destination (where it will be saved) dest = Quartz.CGImageDestinationCreateWithURL( url, LaunchServices.kUTTypeJPEG, # file type 1, # 1 image in file None ) # Add the image to the destination, with certain properties Quartz.CGImageDestinationAddImage(dest, imageOut, properties) # finalize the CGImageDestination object. Quartz.CGImageDestinationFinalize(dest) #For testing how long it takes to take screenshot stop = time.time() # print 'took ' + str(height) + 'px image in ' + str(stop-start)[:5] + ' seconds' except KeyboardInterrupt: print "Keyboard interrupt" AppHelper.stopEventLoop() except errno.ENOSPC: NSLog("No space left on storage device. Turning off Selfspy recording.") self.delegate.toggleLogging_(self) except: NSLog("couldn't save image")
def start_move_listener(self): mask = (NSMouseMovedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.move_handler)
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyUpMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, sc.handle_keyevent ) sc.log('Event match mask and hander set')
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyUpMask | NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, onEvent)
def start_key_listener(self): #TODO may only need the keydown mask, rather than all three mask = (NSKeyDownMask | NSKeyUpMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( mask, self.key_handler)
def applicationDidFinishLaunching_(self, notification): mask1 = NSKeyDownMask mask2 = NSLeftMouseUpMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask1, handler) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask2, handler)
def start_key_listener(self): #TODO may only need the keydown mask, rather than all three mask = (NSKeyDownMask | NSKeyUpMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.key_handler)
def applicationDidFinishLaunching_(self, aNotification): print 'loaded' NSEvent.addGlobalMonitorForEventsMatchingMask_handler_( NSKeyDownMask, handler)
def applicationDidFinishLaunching_(self, notification): downMask = NSKeyDownMask upMask = NSKeyUpMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(downMask, downHandler) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(upMask, upHandler)
def handler(self, event): try: recording = NSUserDefaultsController.sharedUserDefaultsController().values().valueForKey_('recording') if(recording): # get list of apps with regular activation activeApps = self.workspace.runningApplications() regularApps = [] for app in activeApps: if app.activationPolicy() == 0: regularApps.append(app) # get a list of all named windows associated with regular apps # including all tabs in Google Chrome regularWindows = [] options = kCGWindowListOptionAll windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) chromeChecked = False safariChecked = False for window in windowList: window_name = str(window.get('kCGWindowName', u'').encode('ascii', 'replace')) owner = window['kCGWindowOwnerName'] url = 'NO_URL' geometry = window['kCGWindowBounds'] windows_to_ignore = ["Focus Proxy", "Clipboard"] for app in regularApps: if app.localizedName() == owner: if (window_name and window_name not in windows_to_ignore): if owner == 'Google Chrome' and not chromeChecked: s = NSAppleScript.alloc().initWithSource_("tell application \"Google Chrome\" \n set tabs_info to {} \n set window_list to every window \n repeat with win in window_list \n set tab_list to tabs in win \n repeat with t in tab_list \n set the_title to the title of t \n set the_url to the URL of t \n set the_bounds to the bounds of win \n set t_info to {the_title, the_url, the_bounds} \n set end of tabs_info to t_info \n end repeat \n end repeat \n return tabs_info \n end tell") tabs_info = s.executeAndReturnError_(None) # Applescript returns list of lists including title and url in NSAppleEventDescriptors # https://developer.apple.com/library/mac/Documentation/Cocoa/Reference/Foundation/Classes/NSAppleEventDescriptor_Class/index.html numItems = tabs_info[0].numberOfItems() for i in range(1, numItems+1): window_name = str(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(1).stringValue().encode('ascii', 'replace')) url = str(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(2 ).stringValue()) x1 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(1).stringValue()) y1 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(2).stringValue()) x2 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(3).stringValue()) y2 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(4).stringValue()) regularWindows.append({'process': 'Google Chrome', 'title': window_name, 'url': url, 'geometry': {'X':x1,'Y':y1,'Width':x2-x1,'Height':y2-y1} }) chromeChecked = True elif owner == 'Safari' and not safariChecked: s = NSAppleScript.alloc().initWithSource_("tell application \"Safari\" \n set tabs_info to {} \n set winlist to every window \n repeat with win in winlist \n set ok to true \n try \n set tablist to every tab of win \n on error errmsg \n set ok to false \n end try \n if ok then \n repeat with t in tablist \n set thetitle to the name of t \n set theurl to the URL of t \n set thebounds to the bounds of win \n set t_info to {thetitle, theurl, thebounds} \n set end of tabs_info to t_info \n end repeat \n end if \n end repeat \n return tabs_info \n end tell") tabs_info = s.executeAndReturnError_(None) print tabs_info numItems = tabs_info[0].numberOfItems() for i in range(1, numItems+1): window_name = str(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(1).stringValue().encode('ascii', 'replace')) url = str(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(2 ).stringValue()) x1 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(1).stringValue()) y1 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(2).stringValue()) x2 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(3).stringValue()) y2 = int(tabs_info[0].descriptorAtIndex_(i).descriptorAtIndex_(3).descriptorAtIndex_(4).stringValue()) regularWindows.append({'process': 'Safari', 'title': window_name, 'url': url, 'geometry': {'X':x1,'Y':y1,'Width':x2-x1,'Height':y2-y1} }) else: regularWindows.append({'process': owner, 'title': window_name, 'url': url, 'geometry': geometry}) # get active app, window, url and geometry # only track for regular apps for app in regularApps: if app.isActive(): for window in windowList: if (window['kCGWindowNumber'] == event.windowNumber() or (not event.windowNumber() and window['kCGWindowOwnerName'] == app.localizedName())): geometry = window['kCGWindowBounds'] # get browser_url browser_url = 'NO_URL' if (window.get('kCGWindowOwnerName') == 'Google Chrome'): s = NSAppleScript.alloc().initWithSource_("tell application \"Google Chrome\" \n return URL of active tab of front window as string \n end tell") browser_url = s.executeAndReturnError_(None) if (window.get('kCGWindowOwnerName') == 'Safari'): s = NSAppleScript.alloc().initWithSource_("tell application \"Safari\" \n set theURL to URL of current tab of window 1 \n end tell") browser_url = s.executeAndReturnError_(None) browser_url = str(browser_url[0])[33:-3] # browser_url = urlparse(browser_url).hostname if not browser_url: browser_url = 'NO_URL' # call screen hook self.screen_hook(window['kCGWindowOwnerName'], window.get('kCGWindowName', u'').encode('ascii', 'replace'), geometry['X'], geometry['Y'], geometry['Width'], geometry['Height'], browser_url, regularApps, regularWindows) break break loc = NSEvent.mouseLocation() if event.type() == NSLeftMouseDown: self.mouse_button_hook(1, loc.x, loc.y) # elif event.type() == NSLeftMouseUp: # self.mouse_button_hook(1, loc.x, loc.y) elif event.type() == NSRightMouseDown: self.mouse_button_hook(3, loc.x, loc.y) # elif event.type() == NSRightMouseUp: # self.mouse_button_hook(2, loc.x, loc.y) elif event.type() == NSScrollWheel: if event.deltaY() > 0: self.mouse_button_hook(4, loc.x, loc.y) elif event.deltaY() < 0: self.mouse_button_hook(5, loc.x, loc.y) if event.deltaX() > 0: self.mouse_button_hook(6, loc.x, loc.y) elif event.deltaX() < 0: self.mouse_button_hook(7, loc.x, loc.y) # if event.deltaZ() > 0: # self.mouse_button_hook(8, loc.x, loc.y) # elif event.deltaZ() < 0: # self.mouse_button_hook(9, loc.x, loc.y) elif event.type() == NSKeyDown: flags = event.modifierFlags() modifiers = [] # OS X api doesn't care it if is left or right if flags & NSControlKeyMask: modifiers.append('Ctrl') if flags & NSAlternateKeyMask: modifiers.append('Alt') if flags & NSCommandKeyMask: modifiers.append('Cmd') if flags & (NSShiftKeyMask | NSAlphaShiftKeyMask): modifiers.append('Shift') character = event.charactersIgnoringModifiers() # these two get a special case because I am unsure of # their unicode value if event.keyCode() is 36: character = "Enter" if modifiers == ['Cmd', 'Shift']: self.delegate.bookmark_(self) elif event.keyCode() is 42: if modifiers == ['Cmd', 'Shift']: self.delegate.toggleAudioRecording_(self) elif event.keyCode() is 51: character = "Backspace" self.key_hook(event.keyCode(), modifiers, keycodes.get(character, character), event.isARepeat()) elif event.type() == NSMouseMoved: self.mouse_move_hook(loc.x, loc.y) except (SystemExit, KeyboardInterrupt): AppHelper.stopEventLoop() return except: AppHelper.stopEventLoop() raise
def setupKeyCounterMac(self): mask = NSKeyDownMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.macCountKey) NSEvent.addLocalMonitorForEventsMatchingMask_handler_(mask, self.macCountKey)
def applicationDidFinishLaunching_(self, notification): mask = NSKeyDownMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def applicationDidFinishLaunching_(self, notification): mask = (NSKeyDownMask | NSKeyUpMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def applicationDidFinishLaunching_(self, _): mask_down = NSKeyDownMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask_down, key_handler)