I've been trying to describe the issue with your request, but maybe I'm not doing it well:
What is "the frontmost window of monitor 2?" Let's say your app used order, from most recent first, is this:
#1: App 1 • Window 1 • Display 1
#2: App 1 • Window 2 •ㅤ Display 1
#3: App 2 • Window 1 • Display 2
#4: App 3 •ㅤ Window 1 • Display 1
#5: App 4 • Window 1 • Display 2
etc...
I assume you want to toggle between #1 and #3, right? That's the problem I'm trying to describe: It's tricky to figure out that #3 is the window you want. Keyboard Maestro has a %Screen% token that can return dimensions of screens. For my setup, two horizontal displays, I get this:
Main: 0,0,2560,1440
Second: 2560,0,2560,1440
But if my screens were vertical, those values would be:
Main: 0,0,2560,1440
Second: 0,-1440,2560,1440
And if I have one half-overlapped vertically, then it's:
Main: 0,0,2560,1440
Second: 1396,-1440,2560,1440
To switch to the "most recent" window on display 2, you have to get the list of all windows on all displays, in order of use, and then determine which of them is the frontmost one on the second display.
It would have to do that by checking the frame of each window against the frames of your displays, and then checking values. And as you can see above, with different layouts, it's going to get messy figuring out where the second display starts for a given window. (Maybe for others it's clear, but I'm somewhat math-challenged.)
Then once you have that, you need the full frame for both the window on the main display, and the window on the second display, so you can swap them.
But the biggest issue is getting the list of open windows and locations, in order of most recent use. You cannot, to my knowledge, get that list with Keyboard Maestro. I believe you can get it with AppleScript, at least to some degree. And I know you can get it with Swift, as I have a few examples I've built that do just that.
Using that list, and then doing some fancy math that's beyond me, it's probably feasible to write your switcher.
But that's not what's here. What I posted is the only kind of switcher I thought I could write with native Keyboard Maestro actions: It just switches the position (and optionally the size) of the last two used windows, regardless of which displays they're on.
I'll be posting a few other examples in the days to come, one of which lets you pick which window to switch with (by picking it with the mouse); that's probably the closest I'll come to providing a macro that exactly matches what you're looking for. EDIT: But if I make a breakthrough on the display math, maybe I can do better.
-rob.