How to get a string with the name of the window that is focused on python?

4

Some time ago I started using pyHook to handle events. However, the original version has errors, so I changed it to an alternate version that corrected the errors of the original version. However, I have found that this alternate version always shows an empty string for the method that returns the name of the window that is in focus.

from pyHook import HookManager
from python import PumpMessages

def evento_teclado(event):
    print(event.WindowName())
    return True

hm=HookManager()
hm.KeyDown=evento_teclado
hm.HookKeyboard()
PumpMessages()

This script always shows an empty string. Is there any other way to get the window name?

    
asked by anonymous 11.02.2017 / 21:57

1 answer

4

Try to use the win32gui module:

import win32gui window = win32gui.GetWindowText(w.GetForegroundWindow())

    
15.02.2017 / 19:53