torazaemon2016’s blog

手書き文字認識メモ開発

AutohotkeyのスクリプトからHotkeyやHotstringsの一覧表示を作成しダブルクリックやEnterキーでエディタ画面の該当行に行けるahkスクリプト (2026/02 Update)

はじめに

2025/12に公開したHotkeyの一覧が表示できるahkスクリプト

torazaemon2016.hatenablog.jp

Hotkey一覧表示例

をUpdateしました。

更新点は、

  • 選択行のダブルクリックだけではなく、Enterキーでも、エディタの編集画面に飛ぶようにしたことです。

ListViewにてEnterキーを押すことでDoubleClickと同じ処理を行う方法として、 Enterキーを捕捉するため、ボタンのオプションを "Default" にした非表示(枠外、サイズ)のデフォルトボタンを設置する ことで実現しています。

Keyword: ahk, V2, Hotkey, Hotstrings, 一覧表示, VScode

スクリプト

利用したい方は、以下の「エディタ指定」と「一覧にしたいファイルの指定」の2か所の修正を行ってください。

エディタ指定

最初にeditor変数にコマンドへのファイルパスを指定してください。 コマンドラインで行番号を指定して起動できるエディタを利用してください。 現在は、ユーザインストールのVScodeになっています。

;;エディタ指定
;editor := "Notepad.exe"    ; メモ帳は行番号ジャンプできない
;;VScode
;editor := "C:\Program Files\Microsoft VS Code\Code.exe"
editor := "C:\Users\" A_UserName "\AppData\Local\Programs\Microsoft VS Code\Code.exe"
;;notepad++
;editor := "C:\Program Files\Notepad++\notepad++.exe"

エディタによって、指定行番号へ飛ぶという処理が違うので、 スクリプトの最後のほうにあるEdit_file()の中で、ifで判断していますので、そちらもご確認ください。

現在は、VS Codeとnotepad++での定義の記載があります。 そのほかのエディタを利用されている方は、各自で拡張してください。

VScodeの場合:

 options := "--goto `"" A_ScriptDir "\" FileName ":" Line "`""   ; VScode

Notepad++の場合:

 options := "`"" A_ScriptDir "\" Filename "`"" " -n" Line        ; notepad++

一覧にしたいahkスクリプトファイルを指定

関数CreateHotkeyMapGui(*)にて、

CreateHotkeyMapGui(*)
{
    target := A_ScriptDir "\*.ahk"              ; Folder + \ + *.ahk
    ;target := A_ScriptDir "\Autohotkey.ahk"    ; only 1 File

としている部分で、一覧表示するahkファイルを指定しています。

上の場合は、ahkスクリプトのあるフォルダ内の拡張子が.ahkのものすべての一覧を作成することになります。

以下 Autohotkey-ListingHotkeys.ahk

以下のスクリプトを右上のコピーボタンを押して、クリップボードにコピーし、適当なエディタに貼り付けて、 Autohotkey-ListingHotkeys.ahk という名前で保存してください。

Autohotkey V2がインストールされていれば、保存したAutohotkey-ListingHotkeys.ahkをダブルクリックすると、起動されます。 起動したら、「Alt + 4」で一覧ウィンドウが表示されます。

表示された行をダブルクリックすると、エディタが起動して、その行のところで表示されているはずです。

; 2025-11-28 
; Autohotkey Listing Hotkeys and Hotstrings + Functions
; by torazemon2016
; https://www.autohotkey.com/docs/v2/lib/ListView.htm

#Requires AutoHotkey 2.0+
#SingleInstance Force

;;エディタ指定
;editor := "Notepad.exe"    ; メモ帳は行番号ジャンプできない
;;VScode
;editor := "C:\Program Files\Microsoft VS Code\Code.exe"
editor := "C:\Users\" A_UserName "\AppData\Local\Programs\Microsoft VS Code\Code.exe"
;;notepad++
;editor := "C:\Program Files\Notepad++\notepad++.exe"

!4::    ; Autohotkey_V2.ahk
{
    CreateHotkeyMapGui()
}

CreateHotkeyMapGui(*)
{
    target := A_ScriptDir "\*.ahk"              ; Folder + \ + *.ahk
    ;target := A_ScriptDir "\Autohotkey.ahk"    ; only 1 File

    title := "AHK Hotkey List"
    MyGui := Gui("+Resize",title)       ; Allow window maximizing
    MyGui.OnEvent("Close", Gui_Close)   ; _ □ X (Close)
    MyGui.OnEvent("Escape", Gui_Close)   ; Press [ESC] key (Close)
    MyGui.OnEvent("Size", Gui_Size)     ; Resize
    MyGui.SetFont("s16")                ; Font Size

    ; ListView
    LV := MyGui.Add("ListView", "+Grid r20 w1400 h600", ["Filename","Line","Type","Key", "Description"])
    ;LV.OnEvent("Click", Edit_File)         ; 
    LV.OnEvent("DoubleClick", Edit_File)    ; Run Editor

    ; Main
    Load_hotkeys(target)                

    ; The column size is not determined until after the data is loaded, so ModifyCol will be executed later.
    LV.ModifyCol()                      ; Auto-size each column to fit its contents.
    LV.ModifyCol(2, "70 Integer")       ; Line Number
    ;LV.ModifyCol(3, 100)               ; Type (Hotkey,Function,etc)
    LV.ModifyCol(4, 400)                ; Key

    ; 2026-02-04 
    ; Enterキーを捕捉するため、ボタンのオプションを "Default" にした非表示(枠外、サイズ)のデフォルトボタンを設置する
    Btn := MyGui.Add("Button", "Default x-10 y-10 w0 h0", "OK")
    Btn.OnEvent("Click", (*) => Edit_File(LV, LV.GetNext(0, "Focused")))

    MyGui.Show("x0 y200 AutoSize")      ; 

    ;----------------------------
    Load_hotkeys(files)
    {
        Loop Files, files
        {
            ;MsgBox(A_LoopFileName)
            Read_file(A_LoopFileName)
        }
    }

    Read_file(filename)
    {
        Loop read, filename ; Read line by line from a file
        {
            line := A_index

            if RegExMatch(A_LoopReadLine, '^(\s)', &m)  ; If the beginning of the line is blank
                continue                                ; next line

            if(InStr(A_LoopReadLine,"#Include") == 1)   ; ==1 If the first character is #Include
                Store_LV(filename, line, "Include", A_LoopReadLine)
            
            if(InStr(A_LoopReadLine,"::") != 0)         ; Hotkeys and Hotstrings found ('!= 0' means found)
            {
                if(InStr(A_LoopReadLine,";") == 1)      ; If the first character is ;
                    Store_LV(filename, line, "commentout", A_LoopReadLine)
                else if(InStr(A_LoopReadLine,":") == 1) ; If the first character is :
                    Store_LV(filename, line, "Hotstrings", A_LoopReadLine)
                else
                    Store_LV(filename, line, "Hotkey", A_LoopReadLine)
            }
            else if RegExMatch(A_LoopReadLine, '^[a-zA-Z].*\(.*\)', &m) ; Function found 
            {
                if(InStr(A_LoopReadLine,":=") == 0)     ; If it is not an assignment expression ('==0' means not found)
                    Store_LV(filename, line, "Function", A_LoopReadLine)
            }
        }
    }

    Store_LV(filename, line, type, str)
    {
        ;MsgBox(filename " " line " " kind " " str)
        parts := StrSplit(str, ";")
        key := ""
        comment := ""
        if(type == "commentout")    ; ex. ;F1:: ; comment1 -> parts[1] == "" ; parts[2] == F1 ; parts[3] == comment1
        {
            key := parts[2]
            if(parts.Length >= 3)
                comment := parts[3]
             else
                comment := ""
        }
        else                        ; ex. F1::  ; comment1 ; comment2 -> parts[1] == F1 ; parts[2] == comment1 ; parts[3] == comment2
        {
            key := parts[1]
            if(parts.length >= 3)
                comment := parts[2] . " " . ";" . " " . parts[3]
            else if(parts.length == 2)
                comment := parts[2]
            else
                comment := ""
        }

        LV.add(, filename, line, type, key, comment)
    }

    ;----------------------------
    Gui_Close(*)
    { 
        MyGui.Destroy()
    }


    Gui_Size(thisGui, MinMax, Width, Height)  
    {
        if(MinMax == -1)  ; The window has been minimized. No action needed.
            return
        else
            LV.Move(,, Width - 20, Height - 20) ; Expand/Shrink ListView
    }

    ;----------------------------
    Edit_File(LV, RowNumber)
    {
        ;RowText := LV.GetText(RowNumber)
        ;MsgBox("選択された行: " RowNumber "`n内容: " RowText)
        if (RowNumber == 0) ; 行が選択されていない場合は何もしない
            return

        FileName := LV.GetText(RowNumber, 1)
        Line := LV.GetText(RowNumber, 2)
        ;MsgBox(editor " " Filename " " Line , 0x40000)
        try
        {
            options := "`"" A_ScriptDir "\" Filename "`""
            if(InStr(editor,"VS Code") != 0)            ; Match ('!= 0' means found)
            {
                options := "--goto `"" A_ScriptDir "\" FileName ":" Line "`""   ; VScode
            }
            else if(InStr(editor,"notepad++") != 0)
            {
                options := "`"" A_ScriptDir "\" Filename "`"" " -n" Line        ; notepad++
            }
            Run(editor " " options)
        }
        catch
            MsgBox("Could not " editor " " options)
    }

}

Enterキーでエディタに行けるので、カーソルキー上下とEnterでアクセスでき、マウスが必要なくなりました。

ご利用いただき、改良などコメントいただけると幸いです。