AHK versions 1 & 2 in VS Code

I was watching an AutoHotKey video and Version 2 was mentioned, and the fact that it is not backward compatible with Version 1. It is currently in beta stage and its a zip file so you can install it anywhere.

I’m a bit confused about versioning as the current release (v1) has an L next to it, I think this was when they brought out a 64 bit version, so there is the original, the later version and now a version 2. Hmmm!

Anyway I downloaded it and installed it and watched a couple of videos by Tab Nation. In the first he puts the desktop icon file onto the desktop icon of V2 AHK to run, but he then did another video..

Where he renamed the extension to .AHK2 or .AH2 and then associated those extensions with AutoHotKey Version 2 .exe.

So that was great, you could then run either version on the same PC. I then wanted to be able to use the same code editor, in my case VS Code to be able to run either version.

VS Code setup

On looking at VS Code extensions there was one for V1 and one for V2. Also there was a AHK debugger “vscode-autohotkey-debug” by Zero-plusplus- Advanced debugging support for AutoHotkey(includes H) v1 and v2

So you can debug and run both versions.

This extension will not work alone. A separate extension that supports the AutoHotkey language is required(The most famous is slevesque.vscode-autohotkey). If you are using AutoHotkey v2, another extension that supports it required. (e.g. dudelmoser.vscode-autohotkey2).

The way it does it is with the launch.json file it has in your ${workspaceFolder}. This being where you have whichever version of code you are running.

So I have 2 directories AHK-Working & AHK-V2-Working, so I can separate the 2 version codes. Ineach of these directories is a .vscode directory and in that is a launch.json file that sets up the configuration for running files in that particular directory.

I also moved my v2 AHK files to C:/Program Files/AutoHotkey/v2/AutoHotkey.exe and I copied and renamed the AutoHotkey64.exe to just AutoHotkey.exe (that is what it calls in the C:/Program Files/AutoHotkey.exe for the v1 code.

So when you open a file in a specific directory, it will look for the launch.json file to see how it should be setup and which runtime program to use. The code below looks simple ,and the basic settings table looks simple (https://marketplace.visualstudio.com/items?itemName=zero-plusplus.vscode-autohotkey-debug#usage) but it took me a few hours for 3 days before I got the configuration below to work for both instances.

I’ve yet to check out the .ah2 extension, I may have to add a complete block for that also.

Another thing, as launch.json is JS notation you need to be careful with commas, especially at end of 2nd block and last item in list (they don’t have them).

This can be used as the .vscode\ launch.json file in both the 2 directories AHK-Working & AHK-V2-Working for both versions.

    //launch.json
{
    {
        "configurations": [
            {
                "name": "AutoHotkey1 Debug",
                "type": "autohotkey",
                "program":"${file}",
                "request": "launch",
                "runtimeArgs_v1":[".ahk"],
                "port":"9003",
                "runtime_v1": "AutoHotkey.exe",
                "runtimeArgs":[ "/ErrorStdOut=UTF-8"]
                
            },
            {
                "name": "AutoHotkey2 Debug",
                "type": "autohotkey",
                "request": "launch",
                "program":"${file}",
                "runtimeArgs_v2":[".ah2"],
                "runtime_v2": "v2/AutoHotkey64.exe",
                "runtimeArgs":[ "/ErrorStdOut=UTF-8"]
                        },
            {
                 "name": "AutoHotkey2 Debug",
                "type": "autohotkey",
                "request": "launch",
                "runtimeArgs_v2":[".ahk2"],
                "port":"9010",
                "runtime_v2": "v2/AutoHotkey64.exe",
                "program":"${file}",
                "runtimeArgs":[ "/ErrorStdOut=UTF-8"]
                 }
    ]
}

In image above the .ahk file is being run by the v1 AHK program

In image above the .ahk2 file is being run by the v2 AHK program

Issues with VS Code and new setup F5 instead of run arrow

One thing with VS Code now is you have to use the F5 function key to run the fie, whereas before there was an arrow to the right of the ribbon that I always used, so its something I’m going to have to get used to.

V1 to V2 Script Converter

While looking up info about V1 & V2 AutoHotKey I came across a forum item on V1 to V2 Script Converter here and also a github repository with the code. I had to convert extensions from .ahk to .ahk2 and also for any #includes in the code .

I haven’t tried it yet but it looks interesting and may be a handy way to start learning the different structure.

I have a couple of scripts I use all the time and they would be good learning projects and this could be a good tool to start with.

End comment

I’m pleased to have this working now. I’m keen to explore version 2 code and also to write in V2 as well. It took a while.

I’m going to do a video on this as there wasn’t anything too helpful on the net when I went searching.

You have to use functions in V2 so I’m going to have to get the hang of that.

Moving forward its better to get on with the new as that will become more common over time here as the legacy stuff will start to fade.

Also you can just compile the scripts so people can just use that and don’t have to worry about version issues.

Video on the topic :