Well it is not likely a good first time Swift project. It requires a level of integration with the operating system that is highly unusual. It would certainly not result in an application that would be allowed on the App Store. (Think about the security vulnerabilities if the operating system let arbitrary applications silently intercept keyboard input destined for other applications. You would be able to listen to every password as it is typed in.)
However, if you play by the operating system’s rules and do things its way, it is easy enough to get your keyboard to work like that. macOS lets you create custom keyboard layouts (in addition to the hundreds that come preinstalled). Ukelele is an application from SIL that can help you generate custom layouts in a more user‐friendly manner. It cannot do everything the raw XML format can, but is much easier to use for simple changes like what you are suggesting.
You could simply create a layout where the space key produces an underscore. As for toggling it on and off, you can have two keyboards active and set a system hotkey to switch between them. Or you could reinterpret what Caps Lock should do, or only make Shift + Space be an underscore.
The sky is the limit—I can type “¤com” to get ⌘, “¤eu” to get €, or “¤sum” to get ∑ in my keyboard.
But unfortunately none of that helps you practice Swift, so you will have to think of something else for that.