-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWindowTracker.py
47 lines (34 loc) · 1.67 KB
/
WindowTracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import win32gui, time, win32process
from threading import Thread
from Screenshot import Screenshot
from Util import Util
class WindowTracker(Thread):
def __init__(self, config, screenshot):
Thread.__init__(self, name='window tracking')
self.__config = config
self.__screenshot = screenshot
self.__activeWindow = None
self.__screenshotTimer = 0
def run(self):
while True:
if self.__config.windowTrackingIsActive:
time.sleep(self.__config.samplingFrequency)
activeWindow = win32gui.GetForegroundWindow()
activeWindowText = win32gui.GetWindowText(activeWindow)
self.__examineWindow(activeWindowText)
self.__screenshotHandler(activeWindowText)
def __examineWindow(self, activeWindowTitle:str) -> None:
if self.__activeWindow != activeWindowTitle:
self.__activeWindow = activeWindowTitle
windowTitle = '\n'*2 + f'{self.__activeWindow}'.center(100,'-') + '\n'
if self.__config.debug:
print(windowTitle)
Util.fileOut(self.__config.logPath + self.__config.logFileName, windowTitle)
def __screenshotHandler(self, activeWindowText:str) -> None:
for trigger in self.__config.screenshotTrigger.split(' '):
if trigger in activeWindowText.lower():
self.__screenshotTimer += 1
if self.__config.debug:
print(self.__screenshotTimer)
if self.__screenshotTimer % self.__config.screenshotFrequency == 0:
self.__screenshot.takeScreenshot()