Switch wireless keyboard/trackpad while using iMac as external monitor
Check out Synergy.  It's a virtual keyboard/mouse switcher between multiple OS types.  You install the same client on each machine you want to use, and on the "master" machine (server), you configure the virtual arrangement of all the screens.

Very easy to use and set up.  I use it between multiple Macs while they're each booted into their own OS's.  The keyboard/mouse stays "connected" to the "server," so here's how you might use it for your problem:


Download and install Synergy on both machines.
Setup the iMac as the "client."
Setup the MacBook Pro as the "server."
Drag the mouse cursor to the iMac and press Command+F2.


I haven't tested this exact setup myself, but I think it might be a feasable solution.  Best of luck!