LoginSignup
1
0

Visual Studio Codeに於けるAutohotkey v2 スクリプトのデバッグ方法

Last updated at Posted at 2023-08-12

1.はじめに

 ここでは、Visual Studio Codeに於けるAutohotkeyスクリプトのデバッグ方法について説明します。
 Autohotkey v1.1をアンインストールして、Autohotkey v2と最新のv1.1をインストールしたら、visual studio code でデバッグできなくなったので焦りました。何とか対策できたので、備忘録として記載します。

2.PC環境について

 PC環境は以下です。
Autohotkey v1.1.37.01
image.png

Autohotkey v2.0.4
image.png

visual studio code version.1.81.1
image.png

vscode-autohotkey-debug v1.11.0
image.png

3.症状と対策

 デバッグしたいスクリプトが v1.1 と v2.0 とで設定方法が異なりますので、以下に分けて説明します。

3.1.autohotkey v1.1スクリプトのデバッグ

(1)ファイルメニューから「フォルダーをワークスペースに追加」を選択して、スクリプトが格納されたフォルダを指定します。

(2)実行-デバッグの開始を選択すると、警告ダイアログが表示されます。
image.png

(3)'launch.json'を開くボタンをクリックすると、.vscode\launch.jsonが作成されます。
image.png

(4)実行-構成の追加メニューを選択して、Autohotkey Debug(Advanced Configuration) を選択すると、デバッグの設定が追加されます。

image.png

(5) "runtime":"v1.1.37.01/AutoHotkeyU64.exe", を追記します。フォルダ名の区切り文字がスラッシュであること、Autohotkeyのインストールフォルダからの相対パスであることに注意してください。警告ダイアログを参照すると、絶対パス、かつ、フォルダ名の区切り文字はバックスラッシュのように思っていたのですが、違うようです。

image.png

 なお、ANSI版のスクリプトのデバッグには、"runtime":"v1.1.37.01/AutoHotkeyA32.exe", を追記します。
UNICODE版の32bitスクリプトのデバッグには、"runtime":"v1.1.37.01/AutoHotkeyU32.exe", を追記します。

(6) スクリプトにブレークポイントを設定して、実行-デバッグの開始を選択するとデバッグが開始します。この画面は、onScreenKeyboard のサンプルスクリプトの35行目で停止しているところです。
image.png

参考までに、以下にv1.1対応のスクリプト例を記載します。613行もあります。

onScreenKeyboard.ahk (v1.1対応スクリプト)
; On-Screen ANSI Keyboard version 3 (original discussion at https://redd.it/kxh9rb)

; This customizable, non-interactive keyboard only flashes what you type. It may be used to:
;     Memorize the ANSI keyboard format without looking down
;     Record your keystrokes (like in a game or something)
;     Check key functionality on a damaged keyboard
;     Revel in or lament over your typing speed

; /u/KeronCyst added all buttons to the right of Backspace, added a Ctrl-` visibility toggle, cleaned up
; code descriptions, and made the keyboard click-through/non-interactive by default.
; /u/anonymous1184 used invisible characters (to differentiate Numpad numbers from their counterparts) and
; labels (to eliminate version 2's lists of manual character entries).
; Both redditors made aesthetic improvements (symbols + name changes) and fixed various flashing problems.

; Version 2 added color-flashing via progress bars by Lehnemann:
; https://autohotkey.com/board/topic/94703-another-approach-to-a-virtual-keyboard/

; Version 1 by Jon: http://www.autohotkey.com

; Press Ctrl-` or double-click (or right-click) the tray icon to toggle visibility of the keyboard.

; Run a search for and delete the following to make the keyboard:
;  • Clickable (Escape will then close the keyboard if it's focused on):
;        +E0x20
;  • Draggable:
;        -Caption

; Run a search for "Transparency" and edit the number to make it more/less opaque.
; Run a search for "BackgroundRed" and edit "Red" to change the flash color.
; Usable colors: https://www.autohotkey.com/docs/lib/Progress.htm#colors
 
; Changing this font size will resize the keyboard:
k_FontSize = 10
k_FontName = Verdana  ; This can be blank to use the system's default font.
k_FontStyle = Bold    ; Example of an alternative: Italic Underline
 
; Names for the tray menu items:
k_MenuItemHide := "Hide keyboard"
k_MenuItemShow := "Show keyboard"
 
; Put "2" to set the keyboard on monitor 2, etc. (blank = primary monitor):
k_Monitor =

;---- Alter the tray icon menu:
Menu, Tray, Add, %k_MenuItemHide%, k_ShowHide
Menu, Tray, Add, &Exit, k_MenuExit
Menu, Tray, Default, %k_MenuItemHide%
Menu, Tray, NoStandard

;---- Calculate object dimensions based on chosen font size:
k_KeyWidth := k_FontSize * 3
k_KeyHeight := k_FontSize * 3
k_KeyWidth0 := k_KeyWidth * 2

;    Spacing to be used between the keys.
k_KeyMargin := k_FontSize // 6

;    The total width of the keyboard in terms of the keys and their margins.
width := 15 * k_KeyWidth + 14 * k_KeyMargin

;   Values for specially sized keys. The first and last keys of each row are critical for proper sizing.
k_KeyWidthHalf := k_KeyWidth / 2
k_TabW := k_FontSize * 4
k_CapsW := k_KeyWidth + k_KeyMargin + k_KeyWidthHalf
k_ShiftW := 2 * k_KeyWidth + k_KeyMargin
k_SpacebarWidth := k_FontSize * 17
k_LastKeyWidth := width - ( k_TabW + 12 * k_KeyWidth + 13 * k_KeyMargin )
k_EnterWidth := width - ( k_CapsW + 11 * k_KeyWidth + 12 * k_KeyMargin )
k_LastShiftWidth := width - ( k_ShiftW + 10 * k_KeyWidth + 11 * k_KeyMargin )
k_LastCtrlWidth := width - ( 6 * k_TabW + k_SpacebarWidth + 7 * k_KeyMargin )

;   Only a facilitator for creating GUI.
k_KeySize = w%k_KeyWidth% h%k_KeyHeight%
k_KeySize0 = w%k_KeyWidth0% h%k_KeyHeight%
k_Position = x+%k_KeyMargin% %k_KeySize%
k_Numpad0 = x+%k_KeyMargin% %k_KeySize0%

;   This table is used to relate the hotkeys pressed with their progress bars to flash them when pressed.
k_Characters := {"" : ""
    , "``"          :  1
    , 1             :  2
    , 2             :  3
    , 3             :  4
    , 4             :  5
    , 5             :  6
    , 6             :  7
    , 7             :  8
    , 8             :  9
    , 9             : 10
    , 0             : 11
    , "-"           : 12
    , "="           : 13
    , "Backspace"   : 14
    , "Tab"         : 15
    , "Q"           : 16
    , "W"           : 17
    , "E"           : 18
    , "R"           : 19
    , "T"           : 20
    , "Y"           : 21
    , "U"           : 22
    , "I"           : 23
    , "O"           : 24
    , "P"           : 25
    , "["           : 26
    , "]"           : 27
    , "\"           : 28
    , "CapsLock"    : 29
    , "A"           : 30
    , "S"           : 31
    , "D"           : 32
    , "F"           : 33
    , "G"           : 34
    , "H"           : 35
    , "J"           : 36
    , "K"           : 37
    , "L"           : 38
    , ";"           : 39
    , "'"           : 40
    , "Enter"       : 41
    , "LShift"      : 42
    , "Z"           : 43
    , "X"           : 44
    , "C"           : 45
    , "V"           : 46
    , "B"           : 47
    , "N"           : 48
    , "M"           : 49
    , ","           : 50
    , "."           : 51
    , "/"           : 52
    , "RShift"      : 53
    , "LCtrl"       : 54
    , "LWin"        : 55
    , "LAlt"        : 56
    , "Space"       : 57
    , "RAlt"        : 58
    , "RWin"        : 59
    , "AppsKey"     : 60
    , "RCtrl"       : 61
    , "Insert"      : 62
    , "Home"        : 63
    , "PgUp"        : 64
    , "Delete"      : 65
    , "End"         : 66
    , "PgDn"        : 67
    , "Up"          : 68
    , "Left"        : 69
    , "Down"        : 70
    , "Right"       : 71
    , "NumLock"     : 72
    , "NumpadDiv"   : 73
    , "NumpadMult"  : 74
    , "NumpadSub"   : 75
    , "Numpad7"     : 76
    , "Numpad8"     : 77
    , "Numpad9"     : 78
    , "NumpadAdd"   : 79
    , "Numpad4"     : 80
    , "Numpad5"     : 81
    , "Numpad6"     : 82
    , "Numpad1"     : 83
    , "Numpad2"     : 84
    , "Numpad3"     : 85
    , "NumpadEnter" : 86
    , "Numpad0"     : 87
    , "NumpadDot"   : 88 }

zwnbs := Chr(8204) ; Zero-width non-breaking space
labels := { ""     : ""
    , "AppsKey"    : "App"
    , "BackSpace"  : Chr(0x1F844)
    , "CapsLock"   : "Caps"
    , "Delete"     : "Del"
    , "Down"       : Chr(0x2B9F)
    , "End"        : Chr(0x21F2)
    , "Home"       : Chr(0x21F1)
    , "Insert"     : "Ins"
    , "LAlt"       : "Alt"
    , "LCtrl"      : "Ctrl"
    , "Left"       : Chr(0x2B9C)
    , "LShift"     : "Shift"
    , "LWin"       : "Win"
    , "NumLock"    : Chr(0x1F512)
    , "Numpad0"    : "0" zwnbs
    , "Numpad1"    : "1" zwnbs
    , "Numpad2"    : "2" zwnbs
    , "Numpad3"    : "3" zwnbs
    , "Numpad4"    : "4" zwnbs
    , "Numpad5"    : "5" zwnbs
    , "Numpad6"    : "6" zwnbs
    , "Numpad7"    : "7" zwnbs
    , "Numpad8"    : "8" zwnbs
    , "Numpad9"    : "9" zwnbs
    , "NumpadAdd"  : "+"
    , "NumpadDiv"  : "/" zwnbs
    , "NumpadDot"  : "." zwnbs
    , "NumpadEnter": "Ent"
    , "NumpadMult" : "*"
    , "NumpadSub"  : "-" zwnbs
    , "PgDn"       : "PD"
    , "PgUp"       : "PU"
    , "RAlt"       : "Alt" zwnbs
    , "RCtrl"      : "Ctrl" zwnbs
    , "Right"      : Chr(0x2B9E)
    , "RShift"     : "Shift" zwnbs
    , "RWin"       : "Win" zwnbs
    , "Tab"        : Chr(0x2B7E)
    , "Up"         : Chr(0x2B9D) }

;---- Create a GUI window for the on-screen keyboard:
Gui, Font, s%k_FontSize% %k_FontStyle%, %k_FontName%
Gui, +E0x20 -Caption +AlwaysOnTop -MaximizeBox +ToolWindow

;   About keyboards: Tab and Ctrl have the same size, all the buttons on the far right fit to the size of the
;   keyboard (dictated by the first line), Left Shift is the same size as Backspace (on Western keyboards), and
;   the window size is given by x1 + 15 * + 14 * wKey kMargin and y1 + 5 * 4 * + hKey kMargin (where x1 and y1
;   are the coordinates of the first key on the top left of the keyboard).

;   The first row of the virtual keyboard.
Gui, Add, Progress, Section xm ym %k_KeySize% Disabled vprg1
Gui, Add, Progress, %k_Position% Disabled vprg2
Gui, Add, Progress, %k_Position% Disabled vprg3
Gui, Add, Progress, %k_Position% Disabled vprg4
Gui, Add, Progress, %k_Position% Disabled vprg5
Gui, Add, Progress, %k_Position% Disabled vprg6
Gui, Add, Progress, %k_Position% Disabled vprg7
Gui, Add, Progress, %k_Position% Disabled vprg8
Gui, Add, Progress, %k_Position% Disabled vprg9
Gui, Add, Progress, %k_Position% Disabled vprg10
Gui, Add, Progress, %k_Position% Disabled vprg11
Gui, Add, Progress, %k_Position% Disabled vprg12
Gui, Add, Progress, %k_Position% Disabled vprg13
Gui, Add, Progress, x+%k_KeyMargin% w%k_ShiftW% h%k_KeyHeight% Disabled vprg14
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg62 ; Insert
Gui, Add, Progress, %k_Position% Disabled vprg63 ; Home
Gui, Add, Progress, %k_Position% Disabled vprg64 ; PgUp
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg72 ; NumLock
Gui, Add, Progress, %k_Position% Disabled vprg73 ; NumpadDiv
Gui, Add, Progress, %k_Position% Disabled vprg74 ; NumpadMult
Gui, Add, Progress, %k_Position% Disabled vprg75 ; NumpadSub
 
;   The second row.
Gui, Add, Progress, xm y+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg15
Gui, Add, Progress, %k_Position% Disabled vprg16
Gui, Add, Progress, %k_Position% Disabled vprg17
Gui, Add, Progress, %k_Position% Disabled vprg18
Gui, Add, Progress, %k_Position% Disabled vprg19
Gui, Add, Progress, %k_Position% Disabled vprg20
Gui, Add, Progress, %k_Position% Disabled vprg21
Gui, Add, Progress, %k_Position% Disabled vprg22
Gui, Add, Progress, %k_Position% Disabled vprg23
Gui, Add, Progress, %k_Position% Disabled vprg24
Gui, Add, Progress, %k_Position% Disabled vprg25
Gui, Add, Progress, %k_Position% Disabled vprg26
Gui, Add, Progress, %k_Position% Disabled vprg27
Gui, Add, Progress, x+%k_KeyMargin% w%k_LastKeyWidth% h%k_KeyHeight% Disabled vprg28
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg65 ; Delete
Gui, Add, Progress, %k_Position% Disabled vprg66 ; End
Gui, Add, Progress, %k_Position% Disabled vprg67 ; PgDn
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg76 ; Numpad7
Gui, Add, Progress, %k_Position% Disabled vprg77 ; Numpad8
Gui, Add, Progress, %k_Position% Disabled vprg78 ; Numpad9
Gui, Add, Progress, %k_Position% Disabled vprg79 ; NumpadAdd
 
;   The third row.
Gui, Add, Progress, xm y+%k_KeyMargin% w%k_CapsW% h%k_KeyHeight% Disabled vprg29
Gui, Add, Progress, %k_Position% Disabled vprg30
Gui, Add, Progress, %k_Position% Disabled vprg31
Gui, Add, Progress, %k_Position% Disabled vprg32
Gui, Add, Progress, %k_Position% Disabled vprg33
Gui, Add, Progress, %k_Position% Disabled vprg34
Gui, Add, Progress, %k_Position% Disabled vprg35
Gui, Add, Progress, %k_Position% Disabled vprg36
Gui, Add, Progress, %k_Position% Disabled vprg37
Gui, Add, Progress, %k_Position% Disabled vprg38
Gui, Add, Progress, %k_Position% Disabled vprg39
Gui, Add, Progress, %k_Position% Disabled vprg40
Gui, Add, Progress, x+%k_KeyMargin% w%k_EnterWidth% h%k_KeyHeight% Disabled vprg41
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg80 ; Numpad4
Gui, Add, Progress, %k_Position% Disabled vprg81 ; Numpad5
Gui, Add, Progress, %k_Position% Disabled vprg82 ; `
 
;   The fourth row.
Gui, Add, Progress, xm y+%k_KeyMargin% w%k_ShiftW% h%k_KeyHeight% Disabled vprg42
Gui, Add, Progress, %k_Position% Disabled vprg43
Gui, Add, Progress, %k_Position% Disabled vprg44
Gui, Add, Progress, %k_Position% Disabled vprg45
Gui, Add, Progress, %k_Position% Disabled vprg46
Gui, Add, Progress, %k_Position% Disabled vprg47
Gui, Add, Progress, %k_Position% Disabled vprg48
Gui, Add, Progress, %k_Position% Disabled vprg49
Gui, Add, Progress, %k_Position% Disabled vprg50
Gui, Add, Progress, %k_Position% Disabled vprg51
Gui, Add, Progress, %k_Position% Disabled vprg52
Gui, Add, Progress, x+%k_KeyMargin% w%k_LastShiftWidth% h%k_KeyHeight% Disabled vprg53
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg68 ; Up
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg83 ; Numpad1
Gui, Add, Progress, %k_Position% Disabled vprg84 ; Numpad2
Gui, Add, Progress, %k_Position% Disabled vprg85 ; Numpad3
Gui, Add, Progress, %k_Position% Disabled vprg86 ; NumpadEnter
 
;   The last row of keys.
Gui, Add, Progress, xm y+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg54
Gui, Add, Progress, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg55
Gui, Add, Progress, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg56
Gui, Add, Progress, x+%k_KeyMargin% w%k_SpacebarWidth% h%k_KeyHeight% Disabled vprg57
Gui, Add, Progress, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg58
Gui, Add, Progress, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg59
Gui, Add, Progress, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight% Disabled vprg60
Gui, Add, Progress, x+%lastPos% w%k_LastCtrlWidth% h%k_KeyHeight% Disabled vprg61
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled vprg69 ; Left
Gui, Add, Progress, %k_Position% Disabled vprg70 ; Down
Gui, Add, Progress, %k_Position% Disabled vprg71 ; Right
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Numpad0% Disabled vprg87 ; Numpad0
Gui, Add, Progress, %k_Position% Disabled vprg88 ; NumpadDot
 
;---- Add a button for each key. Position the first button with absolute
; coordinates so that all other buttons can be positioned relative to it:

;   The first row of the virtual keyboard.
Gui, Add, Button, section xm ym %k_KeySize%, ``
Gui, Add, Button, %k_Position%, 1
Gui, Add, Button, %k_Position%, 2
Gui, Add, Button, %k_Position%, 3
Gui, Add, Button, %k_Position%, 4
Gui, Add, Button, %k_Position%, 5
Gui, Add, Button, %k_Position%, 6
Gui, Add, Button, %k_Position%, 7
Gui, Add, Button, %k_Position%, 8
Gui, Add, Button, %k_Position%, 9
Gui, Add, Button, %k_Position%, 0
Gui, Add, Button, %k_Position%, -
Gui, Add, Button, %k_Position%, =
Gui, Add, Button, x+%k_KeyMargin% w%k_ShiftW% h%k_KeyHeight%, % labels["Backspace"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, Ins
Gui, Add, Button, %k_Position%, % labels["Home"]
Gui, Add, Button, %k_Position%, % labels["PgUp"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, % labels["NumLock"]
Gui, Add, Button, %k_Position%, % labels["NumpadDiv"]
Gui, Add, Button, %k_Position%, *
Gui, Add, Button, %k_Position%, % labels["NumpadSub"]
 
;   The second row.
Gui, Add, Button, xm y+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["Tab"]
Gui, Add, Button, %k_Position%, Q
Gui, Add, Button, %k_Position%, W
Gui, Add, Button, %k_Position%, E
Gui, Add, Button, %k_Position%, R
Gui, Add, Button, %k_Position%, T
Gui, Add, Button, %k_Position%, Y
Gui, Add, Button, %k_Position%, U
Gui, Add, Button, %k_Position%, I
Gui, Add, Button, %k_Position%, O
Gui, Add, Button, %k_Position%, P
Gui, Add, Button, %k_Position%, [
Gui, Add, Button, %k_Position%, ]
Gui, Add, Button, x+%k_KeyMargin% w%k_LastKeyWidth% h%k_KeyHeight%, \
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, Del
Gui, Add, Button, %k_Position%, % labels["End"]
Gui, Add, Button, %k_Position%, % labels["PgDn"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, % labels["Numpad7"]
Gui, Add, Button, %k_Position%, % labels["Numpad8"]
Gui, Add, Button, %k_Position%, % labels["Numpad9"]
Gui, Add, Button, %k_Position%, +
 
;   The third row.
Gui, Add, Button, xm y+%k_KeyMargin% w%k_CapsW% h%k_KeyHeight%, % labels["CapsLock"]
Gui, Add, Button, %k_Position%, A
Gui, Add, Button, %k_Position%, S
Gui, Add, Button, %k_Position%, D
Gui, Add, Button, %k_Position%, F
Gui, Add, Button, %k_Position%, G
Gui, Add, Button, %k_Position%, H
Gui, Add, Button, %k_Position%, J
Gui, Add, Button, %k_Position%, K
Gui, Add, Button, %k_Position%, L
Gui, Add, Button, %k_Position%, `;
Gui, Add, Button, %k_Position%, '
Gui, Add, Button, x+%k_KeyMargin% w%k_EnterWidth% h%k_KeyHeight%, Enter
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, % labels["Numpad4"]
Gui, Add, Button, %k_Position%, % labels["Numpad5"]
Gui, Add, Button, %k_Position%, % labels["Numpad6"]
 
;   The fourth row.
Gui, Add, Button, xm y+%k_KeyMargin% w%k_ShiftW% h%k_KeyHeight%, % labels["LShift"]
Gui, Add, Button, %k_Position%, Z
Gui, Add, Button, %k_Position%, X
Gui, Add, Button, %k_Position%, C
Gui, Add, Button, %k_Position%, V
Gui, Add, Button, %k_Position%, B
Gui, Add, Button, %k_Position%, N
Gui, Add, Button, %k_Position%, M
Gui, Add, Button, %k_Position%, `,
Gui, Add, Button, %k_Position%, .
Gui, Add, Button, %k_Position%, /
Gui, Add, Button, x+%k_KeyMargin% w%k_LastShiftWidth% h%k_KeyHeight%, % labels["RShift"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, % labels["Up"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, % labels["Numpad1"]
Gui, Add, Button, %k_Position%, % labels["Numpad2"]
Gui, Add, Button, %k_Position%, % labels["Numpad3"]
Gui, Add, Button, %k_Position%, % labels["NumpadEnter"]
 
;   The last row of keys.
Gui, Add, Button, xm y+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["LCtrl"]
Gui, Add, Button, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["LWin"]
Gui, Add, Button, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["LAlt"]
Gui, Add, Button, x+%k_KeyMargin% w%k_SpacebarWidth% h%k_KeyHeight%, Space
Gui, Add, Button, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["RAlt"]
Gui, Add, Button, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["RWin"]
Gui, Add, Button, x+%k_KeyMargin% w%k_TabW% h%k_KeyHeight%, % labels["AppsKey"]
Gui, Add, Button, x+%k_KeyMargin% w%k_LastCtrlWidth% h%k_KeyHeight%, % labels["RCtrl"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Position%, % labels["Left"]
Gui, Add, Button, %k_Position%, % labels["Down"]
Gui, Add, Button, %k_Position%, % labels["Right"]
Gui, Add, Progress, %k_Position% Disabled ; Make some space
Gui, Add, Button, %k_Numpad0%, % labels["Numpad0"]
Gui, Add, Button, %k_Position%, % labels["NumpadDot"]
 
;---- Show the keyboard centered but not active (to maintain the current window's focus):
Gui, Show, xCenter NoActivate, Virtual Keyboard View
 
;   Control whether the virtual keyboard is displayed on the screen or not.
k_IsVisible = y
 
;    Get the window's Width and Height through the GUI's name.
WinGetPos,,, k_WindowWidth, k_WindowHeight, Virtual Keyboard View
 
;---- Position the keyboard at the bottom of the screen while avoiding the taskbar:
SysGet, k_WorkArea, MonitorWorkArea, %k_Monitor%

; Calculate window's X-position:
k_WindowX = %k_WorkAreaRight%
k_WindowX -= %k_WorkAreaLeft%  ; Now k_WindowX contains the width of this monitor.
k_WindowX -= %k_WindowWidth%
k_WindowX /= 2  ; Calculate position to center it horizontally.
; The following is done in case the window will be on a non-primary monitor
; or if the taskbar is anchored on the left side of the screen:
k_WindowX += %k_WorkAreaLeft%

; Calculate window's Y-position:
k_WindowY = %k_WorkAreaBottom%
k_WindowY -= %k_WindowHeight%
 
;   Move the window to the bottom-center position of the monitor.
WinMove, Virtual Keyboard View,, %k_WindowX%, %k_WindowY%
 
;   Make the window transparent (the number regulates the transparency).
WinSet, Transparent, 128, Virtual Keyboard View

; --- Set all keys as hotkeys. See www.asciitable.com
k_n = 1
k_ASCII = 45

Loop {
    ; Change number into a real character.
    k_char := Chr(k_ASCII)

    ; These keys are only accessible using modifier keys; that's why we're escaping them.
    if k_char not in <,>,^,`,,?,:,@
        Hotkey, ~*%k_char%, flashButton
        ; In the above, the asterisk prefix allows the key to be detected regardless
        ; of whether the user is holding down modifier keys such as Control and Shift.
        ; And without "~" the character wouldn't be shown in the window.

    k_ASCII++

    ; Stop looping at the last key of the keyboard ("]").
} until (k_ASCII = 94)

return ; End of auto-execute section.

;---- When a key is pressed by the user, flash the corresponding button on-screen:

;   Fire the corresponding subroutine when we press special + normal keys.
~*`::
~*Backspace::
~*Tab::
~*CapsLock::
~*'::
~*Enter::
~*LShift::
~*,::
~*RShift::
~*LCtrl::  ; Must use Ctrl, not Control, to match button names.
~*LWin::
~*LAlt::
~*Space::
~*RAlt::
~*RWin::
~*AppsKey::
~*RCtrl::

~*Insert::
~*Home::
~*PgUp::
~*Delete::
~*End::
~*PgDn::

~*Up::
~*Left::
~*Down::
~*Right::

~*NumLock::
~*NumpadDiv::
~*NumpadMult::
~*Numpad7::
~*Numpad8::
~*Numpad9::
~*Numpad4::
~*Numpad5::
~*Numpad6::
~*Numpad1::
~*Numpad2::
~*Numpad3::
~*Numpad0::
~*NumpadDot::
~*NumpadSub::
~*NumpadAdd::
~*NumpadEnter::flashButton()


; Show or hide the keyboard if the variable is "y" or "n".
Ctrl & `::
k_ShowHide:
    if k_IsVisible = y
    {
        ; Hide the keyboard gui, change the tray option's name,
        ; and flip visibility.
        Gui, Cancel
        Menu, Tray, Rename, %k_MenuItemHide%, %k_MenuItemShow%
        k_IsVisible = n
    }
    else
    {
        ; Do the opposite.
        Gui, Show
        Menu, Tray, Rename, %k_MenuItemShow%, %k_MenuItemHide%
        k_IsVisible = y
    }
return

;    Function used to flash the button.
flashButton()
{
    ; Erase the key ("~*").
    StringReplace, k_ThisHotkey, A_ThisHotkey, ~*

    ; Prevents the T and B keys from being confused as Tab and Backspace.
    SetTitleMatchMode, 3

    ; Find the variable's index for the control.
    global k_Characters
    index := k_Characters[k_ThisHotkey]

    ; Change the color of the corresponding progress bar to red
    ; (beginning of the flashing's process).
    GuiControl, +BackgroundRed, prg%index%

    ; Wait for the release of the key.
    KeyWait, %k_ThisHotkey%

    ; Remove the flash color.
    GuiControl, -Background, prg%index%

    ; Redraw the button on release (needed because the buttons' names differ from the hotkeys' names).
    global labels
    if (labels.HasKey(k_ThisHotkey))
    {
        GuiControl, MoveDraw, % labels[k_ThisHotkey]
    }
    else
    {
        GuiControl, MoveDraw, % k_ThisHotkey
    }
}

;   Exit the script (via Escape when active, the window's x button, or system tray Exit option).
GuiEscape:
GuiClose:
k_MenuExit:
    ExitApp

3.2.autohotkey v2.0スクリプトのデバッグ

(1)ファイルメニューから「フォルダーをワークスペースに追加」を選択して、スクリプトが格納されたフォルダを指定します。

(2)実行-デバッグの開始を選択すると、警告ダイアログが表示されます。
image.png

(3)'launch.json'を開くボタンをクリックすると、.vscode\launch.jsonが作成されます。
image.png

(4)実行-構成の追加メニューを選択して、Autohotkey Debug(Advanced Configuration) を選択すると、デバッグの設定が追加されます。

image.png

(5)"runtime":"v2/AutoHotkey64.exe", を追記します。フォルダ名の区切り文字がスラッシュであること、Autohotkeyのインストールフォルダからの相対パスであることに注意してください。警告ダイアログを参照すると、絶対パス、かつ、フォルダ名の区切り文字はバックスラッシュのように思っていたのですが、違うようです。

image.png

(6)スクリプトにブレークポイントを設定して、実行-デバッグの開始を選択するとデバッグが開始します。この画面は、onScreenKeyboardの16行目で停止しているところです。

image.png

参考までに、以下にv2対応のスクリプト例を記載します。134行と、コード記載量がかなり減っていることがわかります。

onScreenKeyboard.ahk (v2対応スクリプト)
; On-Screen Keyboard (based on the v1 script by Jon)
; https://www.autohotkey.com
; This script creates a mock keyboard at the bottom of your screen that shows
; the keys you are pressing in real time. I made it to help me to learn to
; touch-type (to get used to not looking at the keyboard). The size of the
; on-screen keyboard can be customized at the top of the script. Also, you
; can double-click the tray icon to show or hide the keyboard.

;---- Configuration Section: Customize the size of the on-screen keyboard and
; other options here.

; Changing this font size will make the entire on-screen keyboard get
; larger or smaller:
k_FontSize := 10
k_FontName := "Verdana"  ; This can be blank to use the system's default font.
k_FontStyle := "Bold"    ; Example of an alternative: Italic Underline

; Names for the tray menu items:
k_MenuItemHide := "Hide on-screen &keyboard"
k_MenuItemShow := "Show on-screen &keyboard"

; To have the keyboard appear on a monitor other than the primary, specify
; a number such as 2 for the following variable. Leave it unset to use
; the primary:
k_Monitor := unset

;---- End of configuration section. Don't change anything below this point
; unless you want to alter the basic nature of the script.

;---- Create a Gui window for the on-screen keyboard:
MyGui := Gui("-Caption +ToolWindow +AlwaysOnTop +Disabled")
MyGui.SetFont("s" k_FontSize " " k_FontStyle, k_FontName)
MyGui.MarginY := 0, MyGui.MarginX := 0

;---- Alter the tray icon menu:
A_TrayMenu.Delete
A_TrayMenu.Add k_MenuItemHide, k_ShowHide
A_TrayMenu.Add "&Exit", (*) => ExitApp()
A_TrayMenu.Default := k_MenuItemHide

;---- Add a button for each key:

; The keyboard layout:
k_Layout := [
    ["``", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace:3"],
    ["Tab:3", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\"],
    ["CapsLock:3", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter:2"],
    ["LShift:3", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "Shift:3"],
    ["LCtrl:2", "LWin:2", "LAlt:2", "Space:2", "RAlt:2", "RWin:2", "AppsKey:2", "RCtrl:2"]
]

; Traverse the keys of the keyboard layout:
for n, k_Row in k_Layout
    for i, k_Key in k_Row
    {
        k_KeyWidthMultiplier := 1
        ; Get custom key width multiplier:
        if RegExMatch(k_Key, "(.+):(\d)", &m)
        {
            k_Key := m[1]
            k_KeyWidthMultiplier := m[2]
        }
        ; Get localized key name:
        k_KeyNameText := GetKeyNameText(k_Key, 0, 1)
        ; Windows key names start with left or right so replace it:
        if (k_Key = "LWin" || k_Key = "RWin")
            k_KeyNameText := "Win"
         ; Truncate the key name:
        if (StrLen(k_Key) > 1)
            k_KeyNameText := Trim(SubStr(k_KeyNameText, 1, 5))
        else
            k_KeyNameText := k_Key
        ; Convert to uppercase:
        k_KeyNameText := StrUpper(k_KeyNameText)
        ; Calculate object dimensions based on chosen font size:
        k_KeyHeight := k_FontSize * 3
        opt := "h" k_KeyHeight " w" k_KeyHeight * k_KeyWidthMultiplier " -Wrap x+m" 
        if (i = 1)
            opt .= " y+m xm"
        ; Add the button:
        Btn := MyGui.Add("Button", opt, k_KeyNameText)
        ; When a key is pressed by the user, click the corresponding button on-screen:
        Hotkey("~*" k_Key, k_KeyPress.bind(Btn))
    }

;---- Position the keyboard at the bottom of the screen (taking into account
; the position of the taskbar):
MyGui.Show("Hide") ; Required to get the window's calculated width and height.
; Calculate window's X-position:
MonitorGetWorkArea(k_Monitor?, &WL,, &WR, &WB)
MyGui.GetPos(,, &k_width, &k_height)
k_xPos := (WR - WL - k_width) / 2 ; Calculate position to center it horizontally.
; The following is done in case the window will be on a non-primary monitor
; or if the taskbar is anchored on the left side of the screen:
k_xPos += WL
; Calculate window's Y-position:
k_yPos := WB - k_height

;---- Show the window:
MyGui.Show("x" k_xPos " y" k_yPos " NA")

;---- Function definitions:
k_KeyPress(BtnCtrl, *)
{ 
    BtnCtrl.Opt("Default") ; Highlight the last pressed key.
    ControlClick(, BtnCtrl,,,, "D")
    KeyWait(SubStr(A_ThisHotkey, 3))
    ControlClick(, BtnCtrl,,,, "U")
}

k_ShowHide(*)
{
    static isVisible := true
    if isVisible
    {
        MyGui.Hide
        A_TrayMenu.Rename k_MenuItemHide, k_MenuItemShow
        isVisible := false
    }
    else
    {
        MyGui.Show
        A_TrayMenu.Rename k_MenuItemShow, k_MenuItemHide
        isVisible := true
    }
}

GetKeyNameText(Key, Extended := false, DoNotCare := false)
{
    Params := (GetKeySC(Key) << 16) | (Extended << 24) | (DoNotCare << 25)
    KeyNameText := Buffer(64, 0)
    DllCall("User32.dll\GetKeyNameText", "Int", Params, "Ptr", KeyNameText, "Int", 32)
    return StrGet(KeyNameText)
}

4.おわりに

 ここでは、Visual Studio Codeに於けるAutohotkeyスクリプトのデバッグ方法について説明しました。Autohotkey v2は、過去のスクリプトとの互換性がなくなった代わりに、シンプルで短く記載でき、変数の衝突も発生しにくくなっていますのでお勧めです。

1
0
2

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
1
0