(Regarding the global button variable: you are of course right. I just did not pay that much attention when creating the example.)
But that it works for you is hard to believe. I have just checked again and made sure that all other addons except the minimal working example are disabled.
For both retail and ptr it is as I have described.
I added
Code:
hooksecurefunc("ToggleGameMenu", function(...)
print("You pressed ESC", ...)
end)
to my code and recorded this "proof" video:
https://youtu.be/Zz03Ttjjqvc
It also shows that once you have opened and closed the map "regularly" (here clicking the close button), ESC works again as expected, until you WorldMapFrame:Hide() it again with my button.