Auto-focus VSCode when Claude Code needs your attention (Windows)

Resolved 💬 3 comments Opened Apr 25, 2026 by Kairos-931 Closed Apr 28, 2026

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'

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗