AutoHotkey 语言 实现系统亮度快捷调节技巧

AutoHotkey阿木 发布于 2025-06-12 15 次阅读


AutoHotkey Language: System Brightness Adjustment via Hotkeys

Introduction

AutoHotkey (AHK) is a powerful scripting language for automating Windows tasks. It allows users to create custom scripts that can automate repetitive tasks, simulate keyboard and mouse inputs, and much more. One of the useful applications of AutoHotkey is to create hotkeys for adjusting system settings, such as brightness. In this article, we will explore how to create an AutoHotkey script that allows users to quickly adjust their system brightness using hotkeys.

Understanding System Brightness Adjustment

Before diving into the code, it's essential to understand how system brightness is controlled on different operating systems.

Windows

On Windows, the brightness level is typically controlled through the registry or the system settings. The brightness level can be accessed using the `NtSetSystemInformation` function, which requires administrative privileges.

macOS

On macOS, the brightness level is controlled through the `IOService` object, which can be accessed using the `IOKit` framework. However, due to security reasons, direct access to the `IOKit` is restricted, and third-party tools are required to adjust brightness.

Linux

On Linux, the brightness level is often controlled through the `brightnessctl` command-line tool, which is part of the `light` package.

AutoHotkey Script for Windows

Below is an example of an AutoHotkey script that allows you to adjust the system brightness on Windows using hotkeys. This script uses the `NtSetSystemInformation` function to change the brightness level.

ahk
; AutoHotkey Script for Adjusting System Brightness on Windows

Persistent
MaxThreadsPerHotkey 2

; Brightness adjustment settings
brightnessStep := 5 ; Adjust this value to change the brightness step size

; Brightness hotkeys
IfWinActive ahk_class Shell_TrayWnd ; Only trigger hotkeys when the system tray is active
^+Up::AdjustBrightness(1) ; Increase brightness
^+Down::AdjustBrightness(-1) ; Decrease brightness
IfWinActive

; Function to adjust brightness
AdjustBrightness(direction) {
brightnessLevel := GetBrightnessLevel()
newLevel := brightnessLevel + (direction brightnessStep)
if (newLevel 100) {
newLevel := 100
}
SetBrightnessLevel(newLevel)
MsgBox, Brightness set to %newLevel%%
}

; Function to get the current brightness level
GetBrightnessLevel() {
; This function needs to be implemented to retrieve the current brightness level
; from the system. For now, it returns a placeholder value.
return 50 ; Placeholder value
}

; Function to set the brightness level
SetBrightnessLevel(level) {
; This function needs to be implemented to set the brightness level using
; the NtSetSystemInformation function. For now, it is a placeholder.
; Note: This function requires administrative privileges.
; Here is a sample implementation for reference:
; VarSetCapacity(BrightnessInfo, 20, 0)
; NumPut(0x00000002, BrightnessInfo, 0, "UInt") ; Brightness level
; NumPut(level, BrightnessInfo, 4, "Int") ; New brightness level
; DllCall("ntdllNtSetSystemInformation", "ptr", &BrightnessInfo, "ptr", 20, "ptr", 0, "ptr", 0)
}

AutoHotkey Script for macOS

Adjusting brightness on macOS using AutoHotkey is more complex due to the restricted access to the `IOKit` framework. However, there are third-party tools like `BrightnessControl` that can be used to adjust brightness. Below is an example script that uses `BrightnessControl` to adjust brightness.

ahk
; AutoHotkey Script for Adjusting System Brightness on macOS

Persistent
MaxThreadsPerHotkey 2

; Brightness hotkeys
IfWinActive ahk_class NSApplication
^+Up::AdjustBrightness(1) ; Increase brightness
^+Down::AdjustBrightness(-1) ; Decrease brightness
IfWinActive

; Function to adjust brightness
AdjustBrightness(direction) {
brightnessLevel := GetBrightnessLevel()
newLevel := brightnessLevel + (direction 10) ; Adjust the step size as needed
if (newLevel 100) {
newLevel := 100
}
SetBrightnessLevel(newLevel)
MsgBox, Brightness set to %newLevel%%
}

; Function to get the current brightness level
GetBrightnessLevel() {
; This function needs to be implemented to retrieve the current brightness level
; from the system. For now, it returns a placeholder value.
return 50 ; Placeholder value
}

; Function to set the brightness level
SetBrightnessLevel(level) {
; This function needs to be implemented to set the brightness level using
; the BrightnessControl tool. For now, it is a placeholder.
; Note: This requires the BrightnessControl tool to be installed and accessible.
Run, brightnesscontrol set %level%
}

AutoHotkey Script for Linux

On Linux, you can use the `brightnessctl` command-line tool to adjust brightness. Below is an example script that uses AutoHotkey to adjust brightness using hotkeys.

ahk
; AutoHotkey Script for Adjusting System Brightness on Linux

Persistent
MaxThreadsPerHotkey 2

; Brightness hotkeys
IfWinActive ahk_class GTK2
^+Up::AdjustBrightness(1) ; Increase brightness
^+Down::AdjustBrightness(-1) ; Decrease brightness
IfWinActive

; Function to adjust brightness
AdjustBrightness(direction) {
brightnessLevel := GetBrightnessLevel()
newLevel := brightnessLevel + (direction 10) ; Adjust the step size as needed
if (newLevel 100) {
newLevel := 100
}
SetBrightnessLevel(newLevel)
MsgBox, Brightness set to %newLevel%%
}

; Function to get the current brightness level
GetBrightnessLevel() {
brightnessLevel := Run("brightnessctl get",, "Hide")
brightnessLevel := SubStr(brightnessLevel, 1, InStr(brightnessLevel, ":") - 1)
return brightnessLevel
}

; Function to set the brightness level
SetBrightnessLevel(level) {
Run("brightnessctl set " level)
}

Conclusion

In this article, we explored how to create AutoHotkey scripts for adjusting system brightness on Windows, macOS, and Linux. By using hotkeys, users can quickly and easily adjust their system brightness without having to navigate through the system settings. While the implementation details may vary depending on the operating system, the basic principles remain the same. With a bit of customization, these scripts can be tailored to fit your specific needs and preferences.