My shell-script solution I've been using for the last year or two:
#!/bin/bash
src="pc"
while true; do
lsusb -d 04d9:a0d1 >/dev/null # Keyboard connected?
ret=$?
if [ $ret -eq 0 -a "$src" != "pc" ]; then
src="pc"
ddcutil --noverify setvcp 60 0x0f # Switch monitor input to DP
elif [ $ret -eq 1 -a "$src" = "pc" ]; then
src="laptop"
ddcutil --noverify setvcp 60 0x11 # Switch monitor input to HDMI
fi
sleep 1
done