无亮度调节键,PowerShell调亮度脚本快捷键启动卡顿求提速方案
优化PowerShell屏幕亮度调节脚本的响应速度
一、优化脚本代码,减少WMI/CIM调用开销
原脚本存在重复调用WMI、未处理亮度边界值的问题,优化后可降低开销并避免无效值:
亮度提升脚本:
# 获取当前亮度并计算新值,限制在0-100区间内 $newBrightness = [Math]::Min(100, (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightness).CurrentBrightness + 10) # 用现代CIM替代旧WMI方法,提升调用效率 (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods).WmiSetBrightness(1, $newBrightness)
亮度降低脚本:
$newBrightness = [Math]::Max(0, (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightness).CurrentBrightness - 10) (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods).WmiSetBrightness(1, $newBrightness)
二、后台常驻PowerShell,监听快捷键(彻底消除启动延迟)
编写一个后台运行的脚本,持续监听快捷键,触发时直接执行调节逻辑,无需每次启动PowerShell:
- 编写常驻脚本(保存为
BrightnessDaemon.ps1):
Add-Type @" using System; using System.Windows.Forms; using System.Runtime.InteropServices; public class HotkeyHandler { public static event EventHandler BrightnessUp; public static event EventHandler BrightnessDown; private static IntPtr hookId = IntPtr.Zero; private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); private static LowLevelKeyboardProc proc = HookCallback; public static void RegisterHotkeys() { hookId = SetHook(proc); Application.Run(); UnhookWindowsHookEx(hookId); } private static IntPtr SetHook(LowLevelKeyboardProc proc) { using (var curProcess = System.Diagnostics.Process.GetCurrentProcess()) using (var curModule = curProcess.MainModule) { return SetWindowsHookEx(13, proc, GetModuleHandle(curModule.ModuleName), 0); } } private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)0x0100) { int vkCode = Marshal.ReadInt32(lParam); bool ctrl = (GetKeyState(0x11) & 0x8000) != 0; bool shift = (GetKeyState(0x10) & 0x8000) != 0; if (ctrl && shift && vkCode == 0xBB) { BrightnessUp?.Invoke(null, EventArgs.Empty); return (IntPtr)1; } if (ctrl && shift && vkCode == 0xBD) { BrightnessDown?.Invoke(null, EventArgs.Empty); return (IntPtr)1; } } return CallNextHookEx(hookId, nCode, wParam, lParam); } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] private static extern short GetKeyState(int keyCode); } "@ -ReferencedAssemblies System.Windows.Forms # 绑定亮度调节事件 [HotkeyHandler]::BrightnessUp += { $newBrightness = [Math]::Min(100, (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightness).CurrentBrightness + 10) (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods).WmiSetBrightness(1, $newBrightness) } [HotkeyHandler]::BrightnessDown += { $newBrightness = [Math]::Max(0, (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightness).CurrentBrightness - 10) (Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods).WmiSetBrightness(1, $newBrightness) } # 启动监听 Write-Host "亮度调节后台服务已启动,按Ctrl+C退出" [HotkeyHandler]::RegisterHotkeys()
- 设置开机自动启动:
- 按Win+R输入
shell:startup打开启动文件夹 - 创建脚本的快捷方式,修改快捷方式目标为:
开机后脚本会静默后台运行,按下powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\你的脚本路径\BrightnessDaemon.ps1"Ctrl+Shift++/Ctrl+Shift+-即可即时调节亮度。
三、将脚本编译为EXE,减少启动时间
使用ps2exe工具把PowerShell脚本编译为独立EXE,消除PowerShell启动开销:
- 安装ps2exe模块:
Install-Module ps2exe -Scope CurrentUser
- 编译脚本:
ps2exe .\BrightnessUp.ps1 .\BrightnessUp.exe -NoConsole ps2exe .\BrightnessDown.ps1 .\BrightnessDown.exe -NoConsole
- 给生成的EXE创建快捷方式并设置快捷键,启动速度会大幅提升。
四、优化快捷方式启动参数
如果不想用常驻或编译,直接修改快捷方式的PowerShell启动参数:
将快捷方式目标从powershell.exe -File "C:\脚本路径\BrightnessUp.ps1"改为:
powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -Command "& 'C:\脚本路径\BrightnessUp.ps1'"
参数说明:
-NoProfile:不加载PowerShell配置文件,减少启动耗时-WindowStyle Hidden:隐藏窗口,避免渲染开销-ExecutionPolicy Bypass:跳过执行策略检查,避免权限阻碍
修改后启动速度可从5秒缩短至1秒左右。
内容的提问来源于stack exchange,提问作者Michiel
相关产品推荐
相关产品推荐

