-1

I write a python program that should run when a certain window is open and in focus.
For this, I need 2 things:

  1. I need to know how to find which window has focus currently.
  2. I need to know if there is a focus change event.

Tried using win32api and win32gui with no success.
How can I do it?

Example to clerify - say I want to write "geronimo" every time I focus notepad.

  • I run the program (notepad closed, nothing happens)
  • I open notepad (it should write geronimo)
  • I change focus to chrome (nothing happens)
  • I change focus back to notepad (writes geronimo again)
GalSuchetzky
  • 785
  • 5
  • 21
  • [SetWinEventHook](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook). – IInspectable Sep 23 '20 at 09:13
  • Similar question: [Obtain Active window using Python - Stack Overflow](https://stackoverflow.com/questions/10266281/obtain-active-window-using-python) – user202729 Jun 03 '23 at 03:04

1 Answers1

0

This script tracks the focus of windows, it can be changed to suit the need. https://gist.github.com/keturn/6695625

GalSuchetzky
  • 785
  • 5
  • 21