How can I get the title of a window where I'm writing in?

(Example: I'm writing something in Notepad (so my program will MsgBox me the name of the window (notepad) , BUT not when I click with the mouse but only when I'm writing something)