Not possible without jailbreaking, and even then I don't know of any way to do it.
It would require a service running in the background which could control system-level remote functions.
Maybe an easier way would be to use a dock with an IR remote that could control it. The dock would need to have the TV output you want too though.