显示 HN:Python 的背光键盘 API
Show HN: Backlit Keyboard API for Python

原始链接: https://github.com/itsmeadarsh2008/backlit-kbd

## backlit-kbd: Python 键盘背光控制 `backlit-kbd` 是一个易于上手的 Python 包,用于在 Linux 上控制键盘背光亮度。它通过 `sysfs` 自动检测兼容硬件,并提供安全的 `--mock` 后端,用于在不影响系统的情况下进行测试。 **主要特性:** * **硬件控制:** 通过百分比或原始级别调整亮度。 * **闪烁:** 支持同步闪烁模式和异步通知式闪烁。 * **易于安装:** 通过 PyPI 安装 (`pip install backlit-kbd`)。 * **Python API:** 提供简单的 API,使用 `create_controller` 实现安全、模拟回退控制。 * **CLI 界面:** 提供命令行界面,用于快速调整和测试。 * **示例:** 包含用于通知闪烁、亮度波形和迪斯科灯的示例脚本。 **用法:** 使用 `--mock` 进行安全实验。对于真实的硬件控制,请确保具有向 `sysfs` 设备写入的适当权限。该软件包还包括全面的测试,并利用 GitHub Actions 和 PyPI Trusted Publishing 进行现代发布实践。

一个新的Python软件包允许开发者控制Linux系统下的背光键盘。该工具由itsmeadarsh2008创建,并在Hacker News上分享,它能够实现自定义通知和效果——例如,当网站宕机时键盘闪烁。 目前,该软件包仅在联想Ideapad游戏笔记本电脑上使用Fedora系统进行了测试,由于测试者键盘的限制,RGB功能尚未验证。MacOS支持正在开发中,Windows兼容性尚未测试。 如果项目获得关注,创作者计划开发一个Rust crate。该项目是开源的,并且可在GitHub上找到([github.com/itsmeadarsh2008](https://github.com/itsmeadarsh2008))。
相关文章

原文

CI Publish PyPI version Python License Downloads Buy Me A Coffee Sponsor

Beginner-friendly Python package to control keyboard backlight brightness.

It supports:

  • Real Linux keyboard backlight devices (auto-discovered from sysfs)
  • A mock backend for safe testing without touching laptop hardware
  • If you use --mock, commands run in memory only (safe for learning).
  • If you do not use --mock, the package tries real hardware automatically.
  • You usually do not need --device-path.

Step 1: Install from PyPI

Step 2 (optional): Install local editable version for development

Step 3 (optional): Install dev dependencies

Step 1: Import

from backlit_kbd import NotificationBlinker, create_controller

Step 2: Create a safe controller (mock fallback)

controller = create_controller(fallback_to_mock=True)

Step 3: Turn on brightness to 60%

Step 4: Blink for a notification

controller.blink(count=3, on_ms=120, off_ms=120, level_percent=100.0)

Step 5: Use async notification manager

blinker = NotificationBlinker(controller)
blinker.start("chat-message", count=5, on_ms=80, off_ms=120)

CLI Guide (Beginner Friendly)

With mock backend:

With real hardware backend:

2) Set Brightness by Raw Level

With mock backend:

With real hardware backend:

3) Set Brightness by Percentage

With mock backend:

backlit-kbd --mock percent 75

With real hardware backend:

Increase by default step (1):

Increase by custom step:

Decrease by custom step:

Real hardware equivalents:

backlit-kbd inc
backlit-kbd inc 2
backlit-kbd dec 2

With mock backend:

backlit-kbd --mock on --percent 40
backlit-kbd --mock off

With real hardware backend:

backlit-kbd on --percent 40
backlit-kbd off

6) Blink Pattern (Synchronous)

With mock backend:

backlit-kbd --mock blink --count 4 --on-ms 100 --off-ms 100 --level-percent 100

With real hardware backend:

backlit-kbd blink --count 4 --on-ms 100 --off-ms 100 --level-percent 100

7) Notification Blink (Async-style Command)

With mock backend:

backlit-kbd --mock notify --name chat --count 5 --on-ms 80 --off-ms 120 --level-percent 100

With real hardware backend:

backlit-kbd notify --name chat --count 5 --on-ms 80 --off-ms 120 --level-percent 100

Global options:

  • --mock Use in-memory backend (safe, no hardware writes)
  • --device-path PATH Optional advanced override for a specific sysfs device

Commands:

  • info
  • set <value>
  • percent <value>
  • inc [step]
  • dec [step]
  • on [--percent N]
  • off
  • blink [--count N --on-ms N --off-ms N --level-percent N]
  • notify [--name NAME --count N --on-ms N --off-ms N --level-percent N]

Scripts:

  • examples/blink_notification.py
  • examples/brightness_wave.py
  • examples/disco_light.py

Run them:

python examples/blink_notification.py
python examples/brightness_wave.py
python examples/disco_light.py

Run tests:

If you are using the project virtual environment:

  1. Permission denied on Linux real hardware mode:
  • You may need proper privileges/udev rules to write to sysfs.
  1. No device found in real hardware mode:
  • Use --mock for learning/testing.
  • Or use create_controller(fallback_to_mock=True) in Python.
  1. Want safe practice mode always:
  • Use --mock in CLI, or force_mock=True / fallback_to_mock=True in code.

This repo has GitHub Actions workflows:

  • CI runs tests on push and pull request
  • Publish builds and publishes to PyPI
  • Uses astral-sh/setup-uv
  • Uses PyPI Trusted Publishing (OIDC)

To publish a new version:

  1. Update version in pyproject.toml
  2. Create and push a tag like v0.1.1
  3. Or publish a GitHub release
  • Start with examples/
  • Check tests in tests/
  • Open issues and PRs

MIT License. Created by Adarsh Gourab Mahalik.

联系我们 contact @ memedata.com