Is it possible to change pin operation from output to input within the Arduino loop (event-driven)?
Yes.

Just call pinMode([pin], [mode]) whenever you want to change from input to output or output to input.

How to decide when to do that is entirely up to you. You can do it any time you choose using any stimulus you like.