Monitorcontrol - Control your monitors through code

I recently acquired an old screen to switch to a dual screen setup. Unfortunately, it does not automatically detect the input source, and I have to manually change it when I want to switch between my desktop and my laptop
Luckily, I also recently learned that you can control a bunch of things on your monitor through code, so let's write a script to help me with that!
Monitorcontrol
Let's briefly see how we can use monitorcontrol, a python package to control our monitor source.
First, we get the model of each monitor:
=
# With this code, my output is:
# - model: VG27A1A
# - model: U2515H
With this information, we can also change the input source for a given monitor. Let's say we want to set the input source of the monitor U2515H
to HDMI1
. We can do this with:
The logic
The logic itself is a small script made of 2 parts. The first one is the configuration part, where I would assign, for each computer, the correct input source for each screen.
For example, my desktop is connected to VG27A1A
through DP1
, and to U2515H
through HDMI2
. For my laptop, it would be HDMI2
for both screens:
=
=
=
The second part is a function taking an enum Computer
as a parameter, which swaps all the monitor sources to this computer:
=
And that's it! With this, I can call set_monitors_source
whenever I want to change from my desktop to my laptop, without touching the screens directly.
Calling the script
I could execute this script via the command line, but I mostly need to call it from a Windows computer, where I rarely have a terminal open.
Instead, I chose to control the app from the systray, to be able to swap computers with only one click. To do this, I grabbed an icon on flaticon, and used pystray for the systray integration.
The code itself is cumbersome, so I won't list it here but can see it on the github page.
Since it also easy to do, I added shortcuts with keyboard to go to computer one or two.
And that's it! I don't have to bother touching the screen anymore, and handling this with a single click i very satisfying.
For more information on the code or to try it out yourself, you can access the complete script available here.