Would it be feasible to reverse the order of targeting/ability selection?
If you select the ability first, then the target for it, it would be rather easy to have the game automatically switch to the appropriate targeting mode, making this whole switching mechanic unneccessary.