For two displays, you could just define the optimal resolution/dpi for each device, and make a script which detects which display is connected, and set the desired resolution/dpi using xrandr with --mode, --dpi and --scale parameters.
For a general solution, check if xrandr | grep connected or sudo get-edid | parse-edid | grep DisplaySize report the correct ...
The closest I can think off is to use the [EDID=*] filter in config.txt BUT this only operates a boot time.
Full documentation is here but in simple terms:
Run tvservice -n from the command line for with monitors to ID the monitor
Add a section for each monitor in config.txt that sets the correct monitor mode:
Download matchbox-keyboard and matchbox-keyboard-im packages (link for Raspbian, if you have Buster, you need .deb files version 0.2 dated 28-Jun-2020). Write installation commands in a text file, e.g.:
sudo dpkg -i matchbox*
<- don't forget the newline after the last command
Put the packages and the text file on the FAT partition of the SD card ...
I tried two different ones: matchbox-keyboard and florence.
You will need a connected USB keyboard to be able to try them. Otherwise, use an SSH connection.
Open a "Terminal" and type sudo apt install matchbox-keyboard. You will find it installed under "Accessories" with the name "Keyboard".
In my opinion, florence is better and ...
I had a similar issue. My script calls the serial ports ttyACM* and I use .config/lxsession/LXDE-pi/autostart to launch it at startup. As I
was having problems with this approach, I checked the .cache/lxsesion/LXDE-pi/rpi.log and I found out my script was not able to open the serial port only at startup, right after boot. It was not a matter of permissions ...