How to share keyboard and mouse in Target Display Mode
I solved this issue using a Griffin PowerMate (basically, a USB-wired button) and some AppleScript to switch Bluetooth off on the iMac, freeing up the keyboard and mouse so my MacBook (connected via TDM) could take over. 

It's very smooth (as smooth as can be for something non-standard). I just connect my MacBook to the iMac, hit cmd+F2 on the keyboard, then tap the button to disable Bluetooth and let the MacBook take charge. When I'm finished, I just unhook TDM, close the MacBook and tap the button again to reconnect the keyboard and mouse to the iMac's Bluetooth.

Details here - hope it's useful to someone else: http://jamesholland.com/post/80600076762/hacking-mac-os-x-to-toggle-bluetooth-for-better-target