How to find out which program running in the foreground in windows, preference in python

Asked

Viewed 274 times

-1

Hello, I need to create a time counter that counts only when pycharm is open in the foreground, and other programs. How do I know which screen is being used in the foreground in Windows? I want to do it in python. Thank you!

1 answer

1

This code shows who is running, it is a loop but serves as an example:

import win32gui
import time
import psutil
import win32process

i = 0

while i <= 1:
    time.sleep(1)
    w = win32gui
    w.GetWindowText (w.GetForegroundWindow())
    pid = win32process.GetWindowThreadProcessId(w.GetForegroundWindow())
    print(psutil.Process(pid[-1]).name())

From then on you can implement your business rule on top of this code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.