Skip to content

Instantly share code, notes, and snippets.

@relalis
Created May 21, 2025 17:17
Show Gist options
  • Select an option

  • Save relalis/e9f95d9323c2f95576dbd009e58dc625 to your computer and use it in GitHub Desktop.

Select an option

Save relalis/e9f95d9323c2f95576dbd009e58dc625 to your computer and use it in GitHub Desktop.
HP Envy k151 HDMI-In switch
# /etc/systemd/system/hdmi-toggle.service
[Unit]
Description=HDMI Button Watcher
[Service]
ExecStart=/usr/local/bin/k151_watch_hdmi_button.py
Restart=always
[Install]
WantedBy=multi-user.target
#!/usr/bin/env python3
# requires python3-evdev on debian systems
# requires ddcutil
from evdev import InputDevice, categorize, ecodes
import subprocess
import re
dev = InputDevice('/dev/input/event2') # Confirm this with `evtest`
for event in dev.read_loop():
if event.type == ecodes.EV_MSC and event.code == ecodes.MSC_SCAN and event.value == 0xff:
output = subprocess.check_output(['/usr/bin/ddcutil', 'getvcp', '0x60', '--terse'])
output = output.decode("utf-8").strip()
match = re.search(r'VCP \d+ SNC x(\d+)', output, re.IGNORECASE)
if match:
current_input = match.group(1)
print(f"Current Input: {current_input}")
if current_input == "11":
subprocess.run(['/usr/bin/ddcutil', 'setvcp', '0x60', '3'])
print("Switched VCP 0x60 to DVI-in")
else:
subprocess.run(['/usr/bin/ddcutil', 'setvcp', '0x60', '17'])
print("Switched VCP 0x60 to HDMI-in")
else:
print(f"ERROR: Could not parse VCP value. `{output}`")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment