Did I get your question right?

1. You need a script that runs after the mouse and keyboard has been unplugged from the PC.

2. And you want the script to hibernate or shut off the PC even if applications like Youtube are still running.

Could you clarify please? Maybe I can help.