You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows系统下使用Python控制笔记本键盘背光灯

Controlling Laptop Keyboard Backlight on Windows with Python

Hey there! I’ve wrestled with this exact problem before—controlling laptop keyboard backlights on Windows using Python can feel like a dead end at first, since there’s no universal, one-size-fits-all API that works across all laptop brands. The key here is that most manufacturers lock this functionality behind their own drivers or utilities, so we need to work with those. Let’s break down the practical, working approaches:

Brand-Specific Tools (Most Reliable)

Laptop makers like Dell, HP, and Lenovo provide command-line tools or DLLs that let you control hardware settings like keyboard brightness. Python can call these tools directly using the subprocess module, or interact with DLLs via ctypes.

Dell Laptops

First, install Dell Command | Monitor (it’s a free utility from Dell’s support site). Once installed, you can use its CLI tool dccmd.exe to adjust brightness:

import subprocess

def set_dell_kb_brightness(level):
    # Make sure dccmd.exe is in your system PATH, or use the full path to it
    try:
        # Brightness level ranges from 0 (off) to 100 (max)
        subprocess.run(
            ["dccmd.exe", "--setkbbrightness", str(level)],
            capture_output=True,
            text=True,
            check=True
        )
        print(f"Keyboard brightness set to {level}% successfully!")
    except subprocess.CalledProcessError as e:
        print(f"Oops, something went wrong: {e.stderr.strip()}")

# Example: Set brightness to 70%
set_dell_kb_brightness(70)

HP Laptops

HP uses the HP System Event Utility, which includes a DLL (hpsystemeventutility.dll) with functions to control keyboard brightness. You can call this DLL directly from Python using ctypes:

import ctypes

# Load the HP System Event DLL
hp_sys_event = ctypes.WinDLL("hpsystemeventutility.dll")

# Define the function signature (check HP's documentation for exact specs)
# Typically, brightness levels range from 0 (off) to 3 (max) for HP laptops
hp_sys_event.SetKeyboardBrightness.argtypes = [ctypes.c_int]
hp_sys_event.SetKeyboardBrightness.restype = ctypes.c_int

def set_hp_kb_brightness(level):
    if not 0 <= level <= 3:
        print("HP brightness levels only go from 0 to 3!")
        return
    result = hp_sys_event.SetKeyboardBrightness(level)
    if result == 0:
        print(f"Keyboard brightness set to level {level} successfully!")
    else:
        print(f"Failed to set brightness. Error code: {result}")

# Example: Set to medium brightness (level 2)
set_hp_kb_brightness(2)

Lenovo Laptops

Lenovo’s Vantage utility has a command-line interface you can leverage. You’ll need to have Lenovo Vantage installed, then use LenovoSettings.exe to adjust brightness:

import subprocess

def set_lenovo_kb_brightness(level):
    # Brightness ranges from 0 to 100
    try:
        subprocess.run(
            ["LenovoSettings.exe", "keyboard", "backlight", "set", str(level)],
            capture_output=True,
            text=True,
            check=True
        )
        print(f"Keyboard brightness set to {level}% successfully!")
    except subprocess.CalledProcessError as e:
        print(f"Error adjusting brightness: {e.stderr.strip()}")

# Example: Set to full brightness
set_lenovo_kb_brightness(100)

Universal HID Approach (For Unsupported Brands)

If your laptop brand doesn’t offer a CLI or DLL, you can try communicating directly with the keyboard via the HID (Human Interface Device) protocol. This requires knowing your keyboard’s Vendor ID (VID) and Product ID (PID), plus the correct HID report format for brightness control.

First, install the hidapi library:

pip install hidapi

Then use this code (replace VID/PID with your keyboard’s values—find them in Device Manager under "Human Interface Devices"):

import hid

# Replace with your keyboard's VID and PID (hex values)
KEYBOARD_VID = 0x1234
KEYBOARD_PID = 0x5678

def set_kb_brightness_hid(level):
    try:
        # Open the HID device
        kb_device = hid.device()
        kb_device.open(KEYBOARD_VID, KEYBOARD_PID)
        
        # Construct the HID report (format varies by manufacturer)
        # Example: Report ID (0x00) + brightness level (0-255) + padding
        report = [0x00] * 64
        report[1] = level  # Adjust this index based on your device's report format
        
        # Send the report to the device
        kb_device.write(report)
        print(f"Brightness set to {level} via HID!")
        
        kb_device.close()
    except Exception as e:
        print(f"Failed to communicate with HID device: {str(e)}")

# Example: Set brightness to half (128)
set_kb_brightness_hid(128)

Important Notes

  • For the HID method, you’ll need to look up your keyboard’s specific HID report format (some manufacturers document this, others require reverse-engineering).
  • Always install the latest manufacturer drivers/utility tools first—they often expose the most reliable ways to control hardware.
  • If none of these work, you might need to write a small C++ helper program that interacts with low-level Windows APIs, then call it from Python. But that’s a more advanced route.

内容的提问来源于stack exchange,提问作者infiniti ayush

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:17:52