Hey Nikita,
See if this doesn't help you get started.
What you want to do is a pretty big job.
Break it down into the smallest possible pieces and try to figure out the pieces.
Here's your basic window changes trigger:
Test Window Trigger.kmmacros (1.8 KB)
When the window changes you test to see if the new window is one you're logging.
If it is then you log the time in a format that can be mathematically added.
Then you trigger another macro that has a time trigger of say 1 minute that tests that value every minute.
If it is less then 5 then it adds a minute.
If it reaches 5 then it fires off a warning and disables itself.
Expect to spend many hours working on this problem.
-Chris