commit add2f959174f89137b34685786c579cc5837d3d3 Author: Valentijn van der Jagt <120188387+HerpieDerpieee@users.noreply.github.com> Date: Mon Nov 24 21:58:38 2025 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..3e75209 --- /dev/null +++ b/main.py @@ -0,0 +1,60 @@ +import time +import wmi +from monitorcontrol import get_monitors + +# MSI G32CQ4 E2 input codes +HDMI1 = 0x11 +DISPLAYPORT = 0x0F + +# The USB name of your keyboard (SteelSeries Apex 5) +KEYBOARD_NAME = "SteelSeries Apex 5" + +def set_input(source_code): + monitors = get_monitors() + if not monitors: + print("No monitors detected.") + return + + with monitors[0] as m: + m.set_input_source(source_code) + +def keyboard_connected(wmi_connection): + """Return True if SteelSeries Apex 5 is currently connected.""" + # Query only devices matching our keyboard name (much faster) + query = f"SELECT Name FROM Win32_PnPEntity WHERE Name LIKE '%{KEYBOARD_NAME}%'" + devices = wmi_connection.query(query) + return len(devices) > 0 + +def main(): + print("Monitoring SteelSeries Apex 5 USB connection...") + + # Create WMI connection ONCE (reuse it) + w = wmi.WMI() + watcher = w.Win32_DeviceChangeEvent.watch_for() + + last_state = None + + while True: + try: + # watcher() blocks until a device event occurs + event = watcher() + + # Check keyboard state (using the same WMI connection) + connected = keyboard_connected(w) + + if connected != last_state: + last_state = connected + + if connected: + print("Keyboard plugged in → Switching to DisplayPort") + set_input(DISPLAYPORT) + else: + print("Keyboard unplugged → Switching to HDMI1") + set_input(HDMI1) + + except Exception as e: + print("Error:", e) + time.sleep(1) + +if __name__ == "__main__": + main() diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..6e833fa --- /dev/null +++ b/run.bat @@ -0,0 +1,3 @@ +@echo off +start /B "" pythonw.exe "C:\Users\valentijn\Desktop\displaySwitcher\main.py" +exit \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..919c126 --- /dev/null +++ b/setup.py @@ -0,0 +1,15 @@ +from cx_Freeze import setup, Executable + +setup( + name="MonitorInputSwitcher", + version="1.0", + description="Monitors keyboard connection and switches input", + options={ + "build_exe": { + "packages": ["wmi", "monitorcontrol", "win32com", "time"], + "excludes": ["tkinter"], + "include_files": [], # add DLLs or files here if needed + } + }, + executables=[Executable("main.py")] +) \ No newline at end of file