import ctypes
import time
user32 = ctypes.windll.user32
def window_title(h_wnd):
title_len = user32.GetWindowTextLengthW(h_wnd) + 1
title_buf = ctypes.create_unicode_buffer(title_len)
user32.GetWindowTextW(h_wnd, title_buf, title_len)
return title_buf.value
while True:
h_wnd = user32.GetForegroundWindow()
title = window_title(h_wnd)
print(title)
time.sleep(1)