Awesome macro, Macro Master Dan!
It seems to work perfectly, but I can't figure out the logic.
But that's OK, you don't need to explain. I'm happy to treat this as a black box.
One thought does occur: This is a great candidate for a custom plug-in, because of its size and complexity. It would be great to have a plug-in that simply returns the number of key presses.