Sure, but giving more context in your original post might have been useful. I misunderstood where you were trying to go with all this. I should have been more explicit when I wrote "I don't know in what sort of context you will want to run such a macro, but I will assume it is for when typing into a document"! I was thinking that you would be just working with character strings when typing text, which I should have spelled out (no pun intended). In fact you wanted something with wider application than just that. And KM is definitely not a good fit.
This really is a relevant tip, then...
The sort of thing you describe is absolutely commonplace for users of programmable keyboards (keyboards that take care of all that low-level stuff when you press or hold a key and then send the resulting keycode to the computer). Here are two methods. I hope this will give you some idea. I have provided links to documentation.
1) space doubling up as a standard MacOS modifier (⇧/⌃/⌥/⌘)
If you just wanted space to double up as the (left, in this example) control ⌃ key when held, you could use:
MT(MOD_LCTL, KC_SPC)
2) space doubling up as a layer switch
You can switch to different layers to get alternative keycodes from keys for as long as the switch is on. There are various ways to do this. What you describe could be handled by, for instance, the following in QMK.
From the documentation:
`LT(layer, kc)` - momentarily activates *layer* when held, and sends *kc* when tapped.
So to switch to the layer called "_LOWER" (don't worry about the name, it's just a conventional name for one of the default layers in QMK) when the key is held, but have it work as a space key when tapped, you would use:
LT(_LOWER, KC_SPC)
A graphical configurator is also available.
You can do the same thing in other systems for flashing keyboard firmware, such as ZMK and Kaleidoscope.
It's kids' stuff – if you are using the right tool for the job. Don't waste time trying to get Keyboard Maestro to do this sort of thing. It's not what it's for. Spend a little time looking into programmable keyboards. Then decide whether it's a rabbit hole that you want to investigate further. For me, it absolutely was, but it will take a significant investment of time and it won't be for everyone.
Perhaps to start with you should try the excellent Karabiner Elements, "A powerful and stable keyboard customizer for macOS". It's been a very long time since I last used it so I am not sure whether it will let you do what you need in this instance. Either way, it will widen your perspective!