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

如何在Python程序中使用PSR的stopevent参数监听录制完成事件

How to Listen for PSR's Post-Save Event Using the stopevent Parameter

Great question! Since PSR's stopevent parameter isn't well-documented, I've put together a tested approach to listen for the post-save event in Python, leveraging Windows' native event objects.

Background

The stopevent parameter tells PSR (Problem Steps Recorder) to trigger a Windows named event object as soon as it finishes recording and saves the output file. This is a cross-process signaling mechanism—we can create the event in Python, pass its name to PSR, and wait for PSR to trigger it once the save is done.

Prerequisites

First, install the pywin32 library to interact with Windows API from Python:

pip install pywin32

Step-by-Step Implementation

1. Create a Named Cross-Process Event

We need an event that PSR can access across process boundaries. Use win32event.CreateEvent to set this up:

  • Add a Global\\ prefix to the event name to ensure it's visible in the global namespace (critical if PSR runs with different user permissions).
  • Set the event to manual reset so we can handle it cleanly after it's triggered.

2. Launch PSR with the stopevent Parameter

Start PSR via subprocess, passing the exact event name we created, plus other necessary parameters like the output file path.

3. Wait for the Event Trigger

Use win32event.WaitForSingleObject to block until the event is triggered (or a timeout occurs). Once triggered, you can run your custom post-save logic.

Full Python Code Example

import win32event
import subprocess

def handle_psr_recording():
    # Define a unique global event name to avoid conflicts with other processes
    EVENT_NAME = "Global\\PsrRecordingCompletedEvent"
    
    # Create the named event (initial state: not triggered)
    event_handle = win32event.CreateEvent(
        lpEventAttributes=None,
        bManualReset=True,  # Keep event triggered until we explicitly reset it
        bInitialState=False,
        lpName=EVENT_NAME
    )
    
    if not event_handle:
        print("Failed to create the event object.")
        return
    
    try:
        # Start PSR with stopevent and output configuration
        psr_process = subprocess.Popen(
            [
                "psr.exe",
                "/start",
                f"/stopevent:{EVENT_NAME}",
                "/output:C:\\Your\\Target\\Path\\Recording.zip",
                "/sc 1",  # Capture 1 screenshot per second (optional)
                "/gui 0"  # Run PSR in silent background mode (remove to show UI)
            ],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
        
        print("PSR recording started. Waiting for completion...")
        
        # Wait for the event to trigger (timeout after 300 seconds / 5 minutes)
        wait_status = win32event.WaitForSingleObject(event_handle, 300000)
        
        if wait_status == win32event.WAIT_OBJECT_0:
            print("✅ PSR recording completed and saved successfully!")
            # Add your custom post-save logic here
            # Example: process the zip file, send notifications, etc.
            # process_recording("C:\\Your\\Target\\Path\\Recording.zip")
            
        elif wait_status == win32event.WAIT_TIMEOUT:
            print("⏳ Recording timed out. Stopping PSR...")
            subprocess.run(["psr.exe", "/stop"], check=False)
            
        else:
            print(f"❌ Unexpected wait status: {wait_status}")
            
    finally:
        # Clean up system resources
        win32event.CloseHandle(event_handle)
        # Ensure PSR is terminated if it's still running
        if psr_process.poll() is None:
            psr_process.terminate()
            print("Terminated running PSR process.")

if __name__ == "__main__":
    handle_psr_recording()

Key Notes & Troubleshooting

  • Event Name Uniqueness: Always use a unique event name (like the global one in the example) to avoid collisions with other processes using named events.
  • PSR Parameters:
    • /gui 0 runs PSR in background mode (no visible UI). Remove this flag if you want the user to see the PSR control window.
    • Make sure the /output directory exists—PSR will fail to save if the path is invalid, and won't trigger the event.
  • Trigger Conditions: The event fires only after PSR successfully saves the recording. If the user cancels recording before saving, the event won't be triggered.
  • Permissions: If you're running Python as admin, PSR will also need admin access to trigger the global event (or vice versa) to avoid permission errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:10:56