I'm checking for an active window title every second, as follows:
import ctypes, time
GetForegroundWindow = ctypes.windll.user32.GetForegroundWindow
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
GetWindowText = ctypes.windll.user32.GetWindowTextW
while True:
time.sleep(1)
act_id = GetForegroundWindow()
length = GetWindowTextLength(act_id)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(act_id, buff, length + 1)
print(buff.value)
It works, but printing the title of some windows causes the following error:
UnicodeEncodeError: 'charmap' codec can't encode character '\u017d' in position
0: character maps to <undefined>
How should I solve this encoding error?
This works well. Now for example 'č' is printed as 'c' and that is sufficient for me.