You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Windows下监听屏幕关闭/屏保事件并禁用鼠标的技术方案问询

Alright, let’s cut to the chase—you want to stop your mouse from waking up the screen when it’s turned off or the screensaver’s active, and you’ve already ruled out the lazy workarounds like unplugging it or flipping it over. Let’s get into the technical solutions that actually address your needs.

Windows系统实现方案

1. 动态调整鼠标唤醒权限(PowerShell脚本)

The core idea here is to temporarily disable your mouse’s "Allow this device to wake the computer" setting when the screen turns off or screensaver activates, then re-enable it when you manually wake the screen (e.g., via keyboard).

  • First, grab your mouse’s hardware ID:

    1. Open Device Manager → Expand Mouse and other pointing devices
    2. Right-click your mouse → PropertiesDetails tab
    3. Select Hardware Ids from the dropdown, then copy one of the listed IDs (e.g., HID\VID_046D&PID_C08B&MI_00)
  • Use this PowerShell script (run as administrator):

    # Check if the screen is turned off
    function Test-ScreenOff {
        $monitor = Get-CimInstance Win32_DesktopMonitor
        return $monitor.PowerManagementSupported -and $monitor.PowerManagementCapabilities -contains 3
    }
    
    # Toggle mouse wake capability
    function Set-MouseWakeCapability {
        param([bool]$Enable)
        $mouseHardwareId = "HID\VID_046D&PID_C08B&MI_00" # Replace with your mouse's ID
        $mouseDevice = Get-PnpDevice -InstanceId $mouseHardwareId -Status OK
    
        if ($Enable) {
            Enable-PnpDevice -InstanceId $mouseHardwareId -Confirm:$false
            powercfg /deviceenablewake $mouseDevice.FriendlyName
        } else {
            Disable-PnpDevice -InstanceId $mouseHardwareId -Confirm:$false
            powercfg /devicedisablewake $mouseDevice.FriendlyName
        }
    }
    
    # Loop to monitor screen state
    while ($true) {
        if (Test-ScreenOff) {
            Set-MouseWakeCapability -Enable $false
        } else {
            Set-MouseWakeCapability -Enable $true
        }
        Start-Sleep -Seconds 5
    }
    
    • Note: Make sure your PowerShell execution policy allows scripts (run Set-ExecutionPolicy RemoteSigned as admin first).

2. 拦截系统事件(C#/.NET App)

If you’re comfortable coding, you can build a lightweight app that listens for screen/screensaver events using Windows APIs:

using System;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;

class ScreenWakeBlocker {
    private const int SC_MONITORPOWER = 0xF170;
    private const int MONITOR_OFF = 2;
    private const int SC_SCREENSAVE = 0xF140;

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    static void Main() {
        SystemEvents.SessionSwitch += OnSessionSwitch;
        Console.WriteLine("Running in background... Press any key to exit.");
        Console.ReadKey();
    }

    private static void OnSessionSwitch(object sender, SessionSwitchEventArgs e) {
        if (e.Reason == SessionSwitchReason.SessionLock) {
            DisableMouseWake();
        } else if (e.Reason == SessionSwitchReason.SessionUnlock) {
            EnableMouseWake();
        }
    }

    private static void DisableMouseWake() {
        // Replace "Your Mouse Name" with your actual mouse's friendly name
        Process.Start(new ProcessStartInfo("powercfg", "/devicedisablewake \"Your Mouse Name\"") {
            CreateNoWindow = true,
            UseShellExecute = false
        });
    }

    private static void EnableMouseWake() {
        Process.Start(new ProcessStartInfo("powercfg", "/deviceenablewake \"Your Mouse Name\"") {
            CreateNoWindow = true,
            UseShellExecute = false
        });
    }
}
  • This app runs in the background and adjusts mouse wake permissions automatically when you lock/unlock your session (which aligns with screen off/screensaver triggers).

3. No-Code Alternative: AutoHotkey

For a simpler no-code route, use AutoHotkey to listen for system events and run commands:

; Listen for system commands related to screen and screensaver
OnMessage(0x0112, "HandleSysCommand")

HandleSysCommand(wParam, lParam) {
    ; Trigger when screen turns off
    if (wParam = 0xF170 && lParam = 2) {
        Run, powercfg /devicedisablewake "Your Mouse Name",, Hide
    }
    ; Trigger when screen turns back on
    else if (wParam = 0xF170 && lParam = -1) {
        Run, powercfg /deviceenablewake "Your Mouse Name",, Hide
    }
    ; Trigger when screensaver starts
    else if (wParam = 0xF140) {
        Run, powercfg /devicedisablewake "Your Mouse Name",, Hide
    }
}
  • Save this as a .ahk file, run it with AutoHotkey, and it’ll handle the rest silently in the background.
macOS系统实现方案

On macOS, the approach mirrors Windows—we’ll adjust wake permissions based on screen state using shell scripts:

#!/bin/bash

MOUSE_NAME="Your Mouse Name" # Replace with your mouse's name (check via `systemsetup -listwakeondevices`)

while true; do
    # Check if the display is off
    SCREEN_OFF=$(pmset -g assertions | grep -q "Display is off" && echo 1 || echo 0)
    
    if [ $SCREEN_OFF -eq 1 ]; then
        # Disable mouse wake
        systemsetup -setwakeonnetworkaccess off
        # Alternatively, use hidutil to temporarily disable mouse input (more aggressive)
        # hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x7000000E0}]}'
    else
        # Enable mouse wake
        systemsetup -setwakeonnetworkaccess on
        # Restore mouse input if using hidutil
        # hidutil property --set '{"UserKeyMapping":[]}'
    fi
    
    sleep 5
done
  • Note: macOS requires elevated permissions for these commands, so run the script with sudo, and you may need to grant accessibility permissions to your terminal app.

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

火山引擎 最新活动