Briefly, when the mouse is over one of our guis, the left-click is remapped to a virtual key. When this happens, the program determines where the "click" happened and acts accordingly. All this without stealing focus because the gui never actually never receives a click event.
This module converts any gui to zero-focus. It has limitations, though. If you are only making a keyboard with buttons but no controls that have scrollbars, this will work well. I was thinking of converting ahkosk to using the module, but haven't gotten around to it -- I have more work to do on it yet before it is ready for scrollable controls.
You might look at my code and the source of AHK to get the C++ functions and API calls that are used. Specifically: hotkey creation and use, mousegetpos, and controlsend for starters. In my code, see *LButton, *LButton up, CheckForClick. The zero-focus module is more stripped down and might be better for your purposes. Have fun!