Update - had one instance of lost focus during a period when the script was not running (changed the parameters to have it run longer).
So far only had one thing pop up unexpectedly - Google Drive - though I did not notice a loss of focus at that time as I may have been clicking from one app to another at the same time.
I am turning off Google Drive for awhile to see what happens, that was only on one of my two systems.
I am thinking about a modification to the code where instead of showing output every tick comparing the new value to the old value and then only showing output when there is a change of focus - that way it could run for much longer and have a smaller output that is easier to see what happens - of course then it might be harder to detect an unwanted change since most entries would be alone - whereas an unexpected loss of focus while in a given app would be only one or maybe two outputs amidst a large block of whatever you were working on when it happened.
On my work system at least I switch between many apps in the course of doing my work - at home I do run lots of apps but don't need to switch between them nearly as often.