Universal Control: https://support.apple.com/en-us/102459
Synergy: https://symless.com/synergy
The Mac keyboard and trackpad do not have external connectors. A hardware solution would require opening the MacBook and wiring ports and a switch from those devices. There is not a plug and play solution.