Ok first up, I don't have one of these keyboards, so I'm not entirely sure what happens here, but from your description, when you click (or simulate one via controlclick) on the M key buttons in the above screen shot, it changes the keyboard mode (which presumably changes the G-Key functions on the left edge ?).
Assuming this is correct, is that change also persistent ? (i.e after closing LGS, does the mode stay at the new mode.)
Essentially, if the answers to both the above is yes, you have two options that I know of, if not the first should still work anyway.
First (and the easier method) is check out this thread https://autohotkey.com/boards/viewtopic.php?f=5&t=34400
which was about the same thing only attacking it from a different angle. The solution in the end was using LGS's lua scripting with a library you can download included to extend it's native features, to get the end result.
Second, assuming these clicks are persistant, you are correct in what you were thinking about "these G and M keys must send and receive specific signals. If I can send these signals with AHK, everything will be great.". The issue is finding the signals. In the thread I linked above I mentioned about a Razer device that I have and at that time I hadn't worked out how to do it but since then I have. The solution is likely the same only the signal or "message" will be different. Essentially you'll need to use something like "spyxx" or similar to monitor messages when you change the mode, and identify the correct one. You will also need to monitor the correct process (which is likely not the main config window, but one of the background hidden ones that is always running).
The second is definitely the more difficult of the two options, but it could probably be done this way, which would result in code using sendmessage or postmessage with the correct parameters to the correct "window". Personally, I'd go the first option.