How could I detect if a notebook is currently the active window?
This is what I have tried, but it does not work as the value of x is never updated:
EventHandler[ CreateDocument[ {TextCell["Window status:", "Text"], ExpressionCell[Dynamic[x], "Output"]}], {"FocusGained" :> (x = "This window is active"), "FocusLost" :> (x = "This window is not active")}] UPDATE: With the help of Carl's answer, I would like to share the motivation for the question. Without the Enable part, using the FileNameSetter (without making the window active first) would produce a "INTERNAL SELF-TEST ERROR: winModality|c|511" (CASE:4033279). Now I can avoid it:
CreateDocument[{ExpressionCell[ Dynamic[ FileNameSetter[Dynamic[f], Enabled -> SelectedNotebook[] === EvaluationNotebook[]]], "Output"]}, WindowSelected -> False] 