Change the function of caps lock to insert underscores when the spacebar is hit
You should be able to achieve this with Ukelele.  I tried to do this myself but got distracted and then gave up.  The documentation isn't exactly stellar.  

Note:  Ukelele is just a GUI to simplify the process of editing the layouts manually as they are large XML files with slightly ambiguous element names.