How to hit escape key to hide popup

I use official popup sample:

public class TestPopup : EditorWindow
{
    [MenuItem("Test/Popup")]
    static void Init()
    {
        TestPopup win = CreateInstance<TestPopup>();
        win.position = new Rect(Screen.width / 2, Screen.height / 2, 300, 200);
        win.ShowPopup();
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("This is an example of EditorWindow.ShowPopup", EditorStyles.wordWrappedLabel);
        GUILayout.Space(70);
        if (Event.current.type == EventType.keyDown)
        {
            Debug.Log($"Event.current.keyCode:{Event.current.keyCode}");
        }
        if (GUILayout.Button("Agree!")) Close();
    }
}

when I hit esc or any other key in this popup window, not Debug.Log show in output, I hope hit esc to cancel this popup, how should i do?(I think click button to close popup win is too slow)

I find the solution, EditorWindow#ShowPopup cannot capture escape but EditorWindow#Show can capture escape, so use EditorWindow#Show instead of, when lost popup focus, auto close this popup, when focus popup, hit esc close it, and I found OnLostFocus cannot be triggered when EditorWindow visible, but EditorWindow.focusedWindow will be changed, so I can use EditorApplication.update += to instead of OnLostFocus

public class TestPopup : EditorWindow
{
    static TestPopup()
    {
        EditorApplication.update += CloseOnLostFocus;
    }

    static void CloseOnLostFocus()
    {
        if (win != null && focusedWindow && focusedWindow != win)
        {
            win.Close();
            win = null;
        }
    }

    private static TestPopup win;

    [MenuItem("Test/Popup")]
    static void Init()
    {
        win = CreateInstance<TestPopup>();
        win.position = new Rect(Screen.width / 2, Screen.height / 2, 300, 200);
        win.Show();
        win.Focus();
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("This is an example of EditorWindow.ShowPopup", EditorStyles.wordWrappedLabel);
        GUILayout.Space(70);
        if (GUILayout.Button("Agree!")) Close();

        var ev = Event.current;
        if (ev.type == EventType.keyDown && ev.keyCode == KeyCode.Escape)
        {
            Close();
        }
    }
}

Don’t use OnGUI, it’s been deprecated years ago.