0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【改良版】WSL2版 Claude Code にスクショをサクッと渡す操作を 3→2キーに減らした

0
Last updated at Posted at 2026-03-29

その他の環境構築まとめはこちら
Claude Code 環境構築・運用Tipsまとめ

はじめに — まだ1手多い

前回の記事で、WSL2版 Claude Code にスクリーンショットを渡す操作を自動化しました。

しかし使っていると、毎回 Ctrl+Alt+S を押す1手がストレスになります。
スクショを撮ったら、次のキー入力で即座にClaude Codeへファイルパスが入力されてほしい。

そこで、常駐スクリプトによる自動ペーストを作成し、操作を3キーから2キーに減らしました!! って、初めから気付きなさいよ。。

Before/After

操作 キーストローク数
前回 Win+Shift+S → Ctrl+Alt+S → Ctrl+V 3
今回 Win+Shift+S → Ctrl+Alt+V 2

つまり、スクショを撮ったら、あとは1キー操作でClaude Codeにパスが入力されます。

コード

今回は、Win32 APIの RegisterHotKey を使う常駐スクリプトに切り替え、ファイルも1つだけにしました。

screenshot-to-wsl-resident.ps1

PowerShellを常駐させ、Ctrl+Alt+V でスクショのWSLコピーと自動ペーストを実行します。

# screenshot-to-wsl-resident.ps1
# 常駐型:RegisterHotKeyでCtrl+Alt+Vを登録し、フォーカスを奪わずに動作する
#
# 起動: powershell -NoProfile -ExecutionPolicy Bypass -File screenshot-to-wsl-resident.ps1
# 停止: Ctrl+C で終了

# ---- 設定(自分の環境に合わせて書き換える) ----
$winDestPath = '\\wsl$\Ubuntu\home\yourname\screenshots'
$wslDestPath = '/home/yourname/screenshots'
# ------------------------------------------------

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class HotKeyHelper {
    [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
    [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    [DllImport("user32.dll")] public static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

    [StructLayout(LayoutKind.Sequential)]
    public struct MSG {
        public IntPtr hwnd;
        public uint message;
        public IntPtr wParam;
        public IntPtr lParam;
        public uint time;
        public int pt_x;
        public int pt_y;
    }
}
"@

Add-Type -AssemblyName System.Windows.Forms

# Ctrl+Alt+V を登録(MOD_CONTROL=0x2, MOD_ALT=0x1, V=0x56)
$HOTKEY_ID = 1
$registered = [HotKeyHelper]::RegisterHotKey([IntPtr]::Zero, $HOTKEY_ID, 0x3, 0x56)
if (-not $registered) {
    Write-Host "エラー: ホットキー Ctrl+Alt+V の登録に失敗しました" -ForegroundColor Red
    exit 1
}
Write-Host "常駐開始: Ctrl+Alt+V で動作します(Ctrl+C で終了)" -ForegroundColor Green

# スクリーンショットフォルダをレジストリから取得(OneDriveリダイレクト対応)
$regKey = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
$guid = "{B7BEDE81-DF94-4682-A7D8-57A52620B86F}"
$screenshotDir = (Get-ItemProperty $regKey -Name $guid -EA SilentlyContinue).$guid
if ($screenshotDir) {
    $screenshotDir = [Environment]::ExpandEnvironmentVariables($screenshotDir)
} else {
    $screenshotDir = Join-Path ([Environment]::GetFolderPath('MyPictures')) "Screenshots"
}

try {
    $msg = New-Object HotKeyHelper+MSG
    # WM_HOTKEY = 0x0312
    while ([HotKeyHelper]::GetMessage([ref]$msg, [IntPtr]::Zero, 0x0312, 0x0312)) {
        # 最新PNGを取得
        $latestFile = Get-ChildItem $screenshotDir -Filter "*.png" |
            Sort-Object LastWriteTime -Descending | Select-Object -First 1
        if ($null -eq $latestFile) { continue }

        # ファイル名をASCIIにリネーム
        if ($latestFile.BaseName -match '(\d{4})-(\d{2})-(\d{2})\s+(\d{6})') {
            $destName = "ss_$($Matches[1])$($Matches[2])$($Matches[3])_$($Matches[4]).png"
        } else {
            $destName = "ss_$([int](Get-Date $latestFile.LastWriteTime -UFormat %s)).png"
        }

        # WSLにコピー → クリップボードにセット
        try {
            Copy-Item $latestFile.FullName (Join-Path $winDestPath $destName) -Force -ErrorAction Stop
        } catch {
            continue
        }

        $clipText = "@$wslDestPath/$destName"
        Set-Clipboard $clipText

        # キーが離される時間を確保してからCtrl+Vを送信
        Start-Sleep -Milliseconds 500
        [System.Windows.Forms.SendKeys]::SendWait("^v")
    }
} finally {
    [HotKeyHelper]::UnregisterHotKey([IntPtr]::Zero, $HOTKEY_ID)
    Write-Host "ホットキー解除・終了" -ForegroundColor Yellow
}

ファイルはUTF-8 BOM付きで保存してください。 BOMなしだとPowerShellが日本語を正しく読めず、パースエラーになります。Visual Studio Codeの場合、右下のエンコーディング表示をクリック →「Save with Encoding」→「UTF-8 with BOM」を選択します。

セットアップ

1. Snipping Toolの自動保存をONにする

Snipping Toolを開き、設定(歯車アイコン)から「スクリーンショットを自動的に保存する」をONにします。
これにより Win+Shift+S で撮影したスクショが ピクチャ\スクリーンショット へ自動保存されます。

2. ファイルを配置してパスを編集

screenshot-to-wsl-resident.ps1 を任意のフォルダに配置し、冒頭の2行を自分の環境に合わせて編集します。
WSL側にもコピー先ディレクトリを作成しておきます。

mkdir -p ~/screenshots

3. 常駐スクリプトを起動

powershell -NoProfile -ExecutionPolicy Bypass -File screenshot-to-wsl-resident.ps1

「常駐開始: Ctrl+Alt+V で動作します」と表示されたら準備完了です。このウィンドウは閉じないでください。

前回から更新する場合

前回のデスクトップショートカット(.lnk)がある場合は、ホットキーが競合するため削除してください。

使い方(まとめ)

Win+Shift+S  →  スクショ撮影
Ctrl+Alt+V   →  WSLにコピー&Claude Codeに自動ペースト

たった2キーです。

最後に

3→2キーになって、快適度がちょっと上がりました。

でも、FileSystemWatcher でスクリーンショットフォルダを監視すれば、撮影を自動検知して 1キー(Win+Shift+S だけ) にできるはず。。 ただそうなると、意図しないときにクリップボードが上書きされることになるので、誤操作しそう。なやましい。。

とか言っているうちに、WSL2版じゃなく、アプリ版のClaude Codeばかり使いそうです。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?