claude-code - 💡(How to fix) Fix Auto-focus VSCode when Claude Code needs your attention (Windows) [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#53126Fetched 2026-04-26 05:23:43
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

Code Example

[console]::beep(1000,500)

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class FocusHelper {
    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    public static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

    public static void ForceForeground(IntPtr hWnd) {
        if (IsIconic(hWnd)) {
            ShowWindow(hWnd, 9); // SW_RESTORE — only when minimized
        }
        keybd_event(0x12, 0, 2, 0); // Alt release to bypass foreground lock
        SetForegroundWindow(hWnd);
    }
}
"@

$proc = Get-Process -Name "Code" -ErrorAction SilentlyContinue |
    Where-Object { $_.MainWindowHandle -ne 0 } |
    Select-Object -First 1

if ($proc) {
    [FocusHelper]::ForceForeground($proc.MainWindowHandle)
}

---

"hooks": {
    "PermissionRequest": [{
        "hooks": [{
            "type": "command",
            "command": "powershell -ExecutionPolicy Bypass -File ~/.claude/focus-vscode.ps1"
        }]
    }],
    "Stop": [{
        "hooks": [{
            "type": "command",
            "command": "powershell -ExecutionPolicy Bypass -File ~/.claude/focus-vscode.ps1"
        }]
    }]
}
RAW_BUFFERClick to expand / collapse

When running long tasks in Claude Code, I often switch to other windows. The problem: Claude finishes a task or needs permission approval, but I don't notice until I manually check.

Solution: Configure Claude Code hooks to play a beep sound and auto-focus the VSCode window.

How it works

Two hooks are configured:

  • PermissionRequest — fires when Claude needs you to approve an action
  • Stop — fires when Claude finishes a task

Both triggers: beep → bring VSCode to foreground.

Setup (Windows only)

1. Create a PowerShell script

Save as ~/.claude/focus-vscode.ps1:

[console]::beep(1000,500)

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class FocusHelper {
    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    public static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

    public static void ForceForeground(IntPtr hWnd) {
        if (IsIconic(hWnd)) {
            ShowWindow(hWnd, 9); // SW_RESTORE — only when minimized
        }
        keybd_event(0x12, 0, 2, 0); // Alt release to bypass foreground lock
        SetForegroundWindow(hWnd);
    }
}
"@

$proc = Get-Process -Name "Code" -ErrorAction SilentlyContinue |
    Where-Object { $_.MainWindowHandle -ne 0 } |
    Select-Object -First 1

if ($proc) {
    [FocusHelper]::ForceForeground($proc.MainWindowHandle)
}

2. Add hooks to ~/.claude/settings.json

"hooks": {
    "PermissionRequest": [{
        "hooks": [{
            "type": "command",
            "command": "powershell -ExecutionPolicy Bypass -File ~/.claude/focus-vscode.ps1"
        }]
    }],
    "Stop": [{
        "hooks": [{
            "type": "command",
            "command": "powershell -ExecutionPolicy Bypass -File ~/.claude/focus-vscode.ps1"
        }]
    }]
}

Notes

  • Window position is preserved — if VSCode is docked to a side monitor or in a specific layout, it won't be resized or repositioned. Only the foreground focus changes.
  • Minimized windows get restored — if VSCode is minimized to taskbar, it will be restored before focusing.
  • Windows only — the script uses Win32 API. For macOS, you could replace it with a simpler AppleScript: osascript -e 'tell application "Visual Studio Code" to activate'

extent analysis

TL;DR

To address the issue of not noticing when Claude Code finishes a task or needs permission approval, configure Claude Code hooks to play a beep sound and auto-focus the VSCode window by setting up a PowerShell script and modifying the ~/.claude/settings.json file.

Guidance

  • Create a PowerShell script ~/.claude/focus-vscode.ps1 with the provided code to handle the beep sound and window focusing.
  • Modify the ~/.claude/settings.json file to include the hooks for PermissionRequest and Stop events, pointing to the PowerShell script.
  • Ensure the script has the correct execution policy to run without issues.
  • Test the setup by triggering a PermissionRequest or Stop event in Claude Code to verify the beep sound and auto-focus functionality.

Example

The provided PowerShell script and settings.json configuration can be used as a starting point for the implementation.

Notes

  • The solution is currently limited to Windows due to the use of the Win32 API in the PowerShell script.
  • For macOS, an alternative approach using AppleScript could be explored, such as osascript -e 'tell application "Visual Studio Code" to activate'.

Recommendation

Apply the workaround by setting up the PowerShell script and modifying the ~/.claude/settings.json file, as this provides a functional solution to the problem of noticing task completion or permission requests in Claude Code.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING