The most reliable work-around for this bug is to clear your target. You can do this in the following ways:
1) Hit your "Esc" key (you may need to hit this key more than once depending on how many UI windows you currently have open; what you want to see is the target portrait at the top-center of your screen clear/go away)
2) Click on unoccupied space (again, you want to see the target portrait at the top-center of your screen clear/go away)
3) Click on the portrait of one of your allies
4) Click on another enemy
5) Wait for currently targeted enemy to die
Try those steps in that order, and you can stop when you get your controls back.
Also, to ensure your ship doesn't speed off to its death while you are trying to regain control, you should still be able to throttle your inpulse to 0. That way, even though your ship may continue to turn towards that Tac Cube (or what-have-you), you won't get any closer to it while you are trying the work-arounds (note: this does not prevent the enemy from coming closer to you).