I have following code which use infinite loop to check if active window has changed.This program prints the name of current window when active window changes. There must be better way to do this. how can a keep my python code always listening to active window change without infinite loop?
import subprocess
import psutil
pid = subprocess.check_output(["xdotool", "getactivewindow", "getwindowpid"]).decode("utf-8").strip()
prevpid=pid
print(pid)
while(True):
pid = subprocess.check_output(["xdotool", "getactivewindow", "getwindowpid"]).decode("utf-8").strip()
if prevpid!=pid:
process=psutil.Process(int(pid))
processName=process.name()
print("Current Active Process is "+processName)
prevpid=pid