Trigger on switching the input device

I'm using a multi-monitor setup and external keyboard+touchpad on my MacBook Pro.
I'd like to reproduce a behaviour similar to this: Multiscreen Multimouse Demo - YouTube I.e. bluetooth keyboard and trackpad are used on external monitor and MacBook's embedded ones are for the embedded screen.

I thought, I could re-position the mouse by triggering on a specific touchpad or keyboard but I'd need a kind of "any" keyboard/touchpad action rather than a specific one.

Any ideas how it could potentially be implemented?