new vscode extension allows advanced AHK V1/V2 debugging

Scripting and setups with Visual Studio Code (vscode) and AutoHotkey.
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

11 Nov 2020, 08:25

boiler wrote:
11 Nov 2020, 08:13
I have resorted to using ImageSearch to locate it, which works well for my purpose, but that’s returning a position in pixels, not column position.
Yes, I think I came across that when researching - I may have to resort to the same method until some other method is found. ¯\_(ツ)_/¯
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

12 Nov 2020, 10:47

I posed the question on Stack Overflow but it hasn't garnered much interest.

Essentially, I'm trying to determine what the start position is is for a selected block of text, preferably using windows cmd/PowerShell and used in a script variable.

BlockSelect.PNG
BlockSelect.PNG (2.61 KiB) Viewed 9297 times
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

12 Nov 2020, 12:14

fade2gray wrote:
11 Nov 2020, 08:07
a method to determine the column position of the text insertion caret within a VSCode editor.
This isn't the most elegant solution, but it works. The bottom of the editor window displaying the status bar needs to visible when the window is active (i.e., not hanging off the screen or turned off). It requires the Accessible Viewer Library which can be downloaded here.

Code: Select all

#Include, <Acc>

Caret := VsCodeLineColumn()
Msgbox, % Caret.Ln "," Caret.Col
return

VsCodeLineColumn() {
	pCoordMode := A_CoordModeMouse
	CoordMode, Mouse, Screen
	MouseGetPos, px, py
	WinGetPos, x, y, w, h, ahk_exe Code.exe
	WinActivate, ahk_exe Code.exe
	loop {
		MouseMove, x + w - 250 - 30 * A_Index, y + h - 10, 0
		oAcc := Acc_ObjectFromPoint(childID)
	} until RegExMatch(oAcc.accName(childID), "Ln (\d+), Col (\d+)", m)
	MouseMove, px, py, 0
	CoordMode, Mouse, % pCoordMode
	return {Ln: m1, Col: m2}
}

It should be possible to accomplish via reading process memory that would be faster and invisible. I might try getting that to work.
zero-plusplus
Posts: 26
Joined: 23 May 2020, 08:01
Contact:

Re: new vscode extension allows advanced AHK V1/V2 debugging

13 Nov 2020, 02:15

I use machine translation.

My idea for solving that is to embed information like column numbers and so on in the VSCode window title. If it is a window title you can read it with AutoHotkey.

Using VSCode's API it is probably possible to embed the current state of the VSCode, such as the column number, in to the window title.
The bottom bar of VSCode has column numbers updated in real time, so I'm assuming there is an event to monitor the caret movement.


I'm currently working on a `vscode-autohotkey` extension to support all AutoHotkey(including H), and I'm thinking of adding this feature to it.

I plan to release a preview version of this extension as early as this month, or by the end of this year at the latest.
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

13 Nov 2020, 04:46

boiler wrote:
12 Nov 2020, 12:14
The bottom of the editor window displaying the status bar needs to visible...

Excellent, this solution does work for me - just needed to make a couple of adjustments.

  1. I prefer to have my taskbar hidden with the editor open fullscreen. The initial MouseMove y coord, in the Loop{}Until routine, causes the taskbar to be revealed, obscuring the browser status bar, resulting in the mouse to getting trapped in the bottom left corner of the screen. This was resolved by reducing the y coord by an extra pixel to -11.
  2. I sometimes launch scripts from the taskbar, so of course, the editor's status bar gets hidden when open fullscreen. Got around that by wrapping the function call with Lexikos' code snippet that hides/unhides the taskbar.
Thank you.
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

13 Nov 2020, 04:52

zero-plusplus wrote:
13 Nov 2020, 02:15
I'm currently working on a `vscode-autohotkey` extension to support all AutoHotkey(including H), and I'm thinking of adding this feature to it.
Hi, is this your extension vscode-autohotkey-plus-plus? I posted a similar question in the repo.
zero-plusplus
Posts: 26
Joined: 23 May 2020, 08:01
Contact:

Re: new vscode extension allows advanced AHK V1/V2 debugging

13 Nov 2020, 06:16

The `vscode-autohotkey` repository is not yet public.
It will be published on the following accounts.
https://github.com/zero-plusplus
neogna2
Posts: 617
Joined: 15 Sep 2016, 15:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

15 Nov 2020, 06:30

boiler wrote:
12 Nov 2020, 12:14
This isn't the most elegant solution, but it works. The bottom of the editor window displaying the status bar needs to visible when the window is active (i.e., not hanging off the screen or turned off). It requires the Accessible Viewer Library which can be downloaded here.
I don't get the script to work on my PC. In fact Acc doesn't read any content from the VS Code window when I try with jeeswg's JEE_AccGetTextAll function. Is there some accessibility setting to enable in VS Code first?
zero-plusplus wrote:
13 Nov 2020, 02:15
Using VSCode's API it is probably possible to embed the current state of the VSCode, such as the column number, in to the window title.
The bottom bar of VSCode has column numbers updated in real time, so I'm assuming there is an event to monitor the caret movement.
Sounds useful. I'm also looking for a way to get the VS Code cursor X Y screen position that is faster than using ImageSearch to find the cursor. Consider releasing a separate extension with only that functionality. Because there are already multiple competing AutoHotkey extensions with partly overlapping features and that can be tricky to run at the same time.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

15 Nov 2020, 07:18

neogna2 wrote:
15 Nov 2020, 06:30
I don't get the script to work on my PC. In fact Acc doesn't read any content from the VS Code window when I try with jeeswg's JEE_AccGetTextAll function. Is there some accessibility setting to enable in VS Code first?
I don't know. The JEE_AccGetTextAll function and other Acc functions just work for me, and I haven't changed any settings on my system to get them to work.



@fade2gray (@zero-plusplus may also be interested) - Having learned more about Acc, I now have an much better function for getting the VS Code caret's line and column position. It works instantly and invisibly (without activating the window or moving the mouse).

Code: Select all

#Include, <Acc>

Caret := VsCodeLineColumn()
Msgbox, % Caret.Ln "," Caret.Col
return

VsCodeLineColumn() {
	hwnd := WinExist("ahk_exe Code.exe")
	oAcc := Acc_Get("Object", "4.1.2.1.1.2.1.1.1.1.2.3.1.8", 0, "ahk_id " hwnd)
	RegExMatch(oAcc.accName(0), "Ln (\d+), Col (\d+)", m)
	return {Ln: m1, Col: m2}
}
I don't know how stable the object numbering is. If it turns out to change with updates, then I could add something that gets the full object and searches for the item number that contains the line and column information in it before using it. I'd be interested to hear if this version works for you.
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

15 Nov 2020, 07:52

neogna2 wrote:
15 Nov 2020, 06:30
I don't get the script to work on my PC.
Maybe you haven't installed the library file correctly?

This includes the function taken from the library file.

EDIT: Had to update the code to include another libry function call that I missed. :oops:

Code: Select all

Caret := VsCodeLineColumn()
WinShow ahk_class Shell_TrayWnd
WinShow Start ahk_class Button
Msgbox, % "Cursor position is at Line: "Caret.Ln " Column: " Caret.Col
ExitApp

ObjectFromPoint(ByRef _idChild_ = "", x = "", y = ""){
    Init()
    If	DllCall("oleacc\AccessibleObjectFromPoint", "Int64", x==""||y==""?0*DllCall("GetCursorPos","Int64*",pt)+pt:x&0xFFFFFFFF|y<<32, "Ptr*", pacc, "Ptr", VarSetCapacity(varChild,8+2*A_PtrSize,0)*0+&varChild)=0
    Return	ComObjEnwrap(9,pacc,1), _idChild_:=NumGet(varChild,8,"UInt")
}

Init(){
    Static	h
    If Not	h
        h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}

VsCodeLineColumn(){
    pCoordMode := A_CoordModeMouse
    CoordMode, Mouse, Screen
    MouseGetPos, px, py
    WinGetPos, x, y, w, h, ahk_exe Code.exe
    WinActivate, ahk_exe Code.exe
    loop {
        MouseMove, x + w - 250 - 30 * A_Index, y + h - 11, 0
        oAcc := ObjectFromPoint(childID)
    } until RegExMatch(oAcc.accName(childID), "Ln (\d+), Col (\d+)", m)
    MouseMove, px, py, 0
    CoordMode, Mouse, % pCoordMode
    return {Ln: m1, Col: m2}
}
AHK-CursorPos.PNG
AHK-CursorPos.PNG (28.97 KiB) Viewed 9141 times
Last edited by fade2gray on 15 Nov 2020, 08:49, edited 1 time in total.
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

15 Nov 2020, 07:56

boiler wrote:
15 Nov 2020, 07:18
@fade2gray (@zero-plusplus may also be interested) - Having learned more about Acc, I now have an much better function for getting the VS Code caret's line and column position. It works instantly and invisibly (without activating the window or moving the mouse).
Thanks boiler, I shall give that a shot.

EDIT: Nope, doesn't work for me - is it possibly dependent on screen size/resolution?
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

15 Nov 2020, 09:55

It shouldn't have to do with size or resolution, but it may have to do with whatever else happens to be displayed in your VSCode client because it will affect the object structure. Try this version, which is based on jeeswg's JEE_AccGetTextAll(). It's a bit slower because it navigates the entire Acc object structure until it finds the element that contains the Ln/Col info.

Code: Select all

Caret := VsCodeLineColumn()
MsgBox % Caret.Ln "," Caret.Col
return

VsCodeLineColumn()
{
	hwnd := WinExist("ahk_exe Code.exe")
	oMem := {}, oPos := {}
	oMem[1, 1] := Acc_ObjectFromWindow(hwnd, 0x0)
	oPos[1] := 1, vLevel := 1

	loop
	{
		if !vLevel
			break
		if !oMem[vLevel].HasKey(oPos[vLevel])
		{
			oMem.Delete(vLevel)
			oPos.Delete(vLevel)
			vLevelLast := vLevel, vLevel -= 1
			oPos[vLevel]++
			continue
		}
		oKey := oMem[vLevel, oPos[vLevel]]

		vName := "", vValue := ""
		if IsObject(oKey)
		{
			vRoleText := Acc_GetRoleText(oKey.accRole(0))
			try vName := oKey.accName(0)
			try vValue := oKey.accValue(0)
		}
		else
		{
			oParent := oMem[vLevel-1,oPos[vLevel-1]]
			vChildId := IsObject(oKey) ? 0 : oPos[vLevel]
			vRoleText := Acc_GetRoleText(oParent.accRole(vChildID))
			try vName := oParent.accName(vChildID)
			try vValue := oParent.accValue(vChildID)
		}		if (StrLen(vName) > vLimN)

		vAccPath := ""
		if IsObject(oKey)
		{
			Loop, % oPos.Length() - 1
				vAccPath .= (A_Index=1?"":".") oPos[A_Index+1]
		}
		else
		{
			Loop, % oPos.Length() - 2
				vAccPath .= (A_Index=1?"":".") oPos[A_Index+1]
			vAccPath .= " c" oPos[oPos.Length()]
		}
		if RegExMatch(vName, "Ln (\d+), Col (\d+)", m)
			return {Ln: m1, Col: m2}
		oChildren := Acc_Children(oKey)
		if !oChildren.Length()
			oPos[vLevel]++
		else
		{
			vLevelLast := vLevel, vLevel += 1
			oMem[vLevel] := oChildren
			oPos[vLevel] := 1
		}
	}
}
User avatar
fade2gray
Posts: 87
Joined: 21 Apr 2015, 12:28

Re: new vscode extension allows advanced AHK V1/V2 debugging

15 Nov 2020, 10:49

boiler wrote:
15 Nov 2020, 09:55
It's a bit slower because it navigates the entire Acc object structure until it finds the element that contains the Ln/Col info.

Wow, I see what you mean :lol:

Code: Select all

---------------------------
temp-2020-11-15-15-41-45.ahk
---------------------------
TickCount = 2093

Pos = 4,75
---------------------------
OK   
---------------------------
neogna2
Posts: 617
Joined: 15 Sep 2016, 15:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

16 Nov 2020, 05:07

boiler wrote:
15 Nov 2020, 07:18
neogna2 wrote:
15 Nov 2020, 06:30
I don't get the script to work on my PC. In fact Acc doesn't read any content from the VS Code window when I try with jeeswg's JEE_AccGetTextAll function.
I don't know. The JEE_AccGetTextAll function and other Acc functions just work for me, and I haven't changed any settings on my system to get them to work.
Ok. I've retried with both AutoHotkey 32U and 64U and double checked my Acc.ahk version, but still not working. I can use Acc on other application windows, but on VS Code it doesn't get the contents. Tried the VS Code insiders (beta) version, but same problem there. Since this is a general thing with Acc and VS Code on my PC, not only your script, I'll stop commenting about it here and maybe post in the Ask forum section instead.

Here's what the AccViewer shows when I drag the crosshair onto the VS Code window https://imgur.com/a/wXWWHjE
boiler wrote:
15 Nov 2020, 09:55
this version, which is based on jeeswg's JEE_AccGetTextAll(). It's a bit slower because it navigates the entire Acc object structure
In some applications where the Acc path changes the variation can be quite small. For example in Firefox some of the elements in the top toolbar can vary but it is only one number in the path that varies between sessions/instances. In such cases it is quicker to loop over possible values for that number and check the path element until the correct path is found, compared to getting the whole Acc object.
zero-plusplus
Posts: 26
Joined: 23 May 2020, 08:01
Contact:

Re: new vscode extension allows advanced AHK V1/V2 debugging

16 Nov 2020, 05:40

@boiler
I'm interested in it too. I recently learned about Acc but gave up on it because it doesn't work with v2. Caret position is important when automating, so I'd like to port it to v2 if I have the time.

@neogna2
I'll publish it as a separate extension. It will be named `vscode-customize-title`.
I' ll release it in December or in January next year, though it will be delayed because of the priority of `vscode-autohotkey` development.
I'll report here as soon as it's done.

It's probably not possible to get the screen position. A similar question was found on stackoverflow. Can't I get it in Acc?
https://stackoverflow.com/questions/59283972/would-it-be-theoretically-possible-to-get-x-y-screen-coordinates-of-caret-in-vs
Last edited by zero-plusplus on 17 Nov 2020, 23:03, edited 1 time in total.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

16 Nov 2020, 07:46

zero-plusplus wrote:
16 Nov 2020, 05:40
It's probably not possible to get the screen position. A similar question was found on stackoverflow. Can't I get it in Acc?
It looks like it should be possible since accLocation is one of the methods supported for carets.

Reference: https://docs.microsoft.com/en-us/windows/win32/winauto/caret
neogna2
Posts: 617
Joined: 15 Sep 2016, 15:44

Re: new vscode extension allows advanced AHK V1/V2 debugging

16 Nov 2020, 08:07

boiler wrote:
16 Nov 2020, 07:46
It looks like it should be possible since accLocation is one of the methods supported for carets.
Reference: https://docs.microsoft.com/en-us/windows/win32/winauto/caret
That lead me to Acc code by malcev for getting caret position in Chrome. The same approach worked also in VS Code, even on the same PC where the other VS Code Acc script didn't work.

Code: Select all

#Include <Acc>
;Acc method to capture caret X Y screen position in active window
Acc_Caret := Acc_ObjectFromWindow(WinExist("A"), OBJID_CARET := 0xFFFFFFF8)
Caret_Location := Acc_Location(Acc_Caret)
MsgBox, % Caret_Location.x "`n" Caret_Location.y
return
zero-plusplus
Posts: 26
Joined: 23 May 2020, 08:01
Contact:

Re: new vscode extension allows advanced AHK V1/V2 debugging

16 Nov 2020, 11:33

It's great to be able to get the screen position of the caret! I learned something good.


I tried to create a `vscode-customize-title`, but unfortunately it's not useful.

It works fine, but I've encountered another problem: needs to change the `window.title` in settings.json to change the title.
This means that if you move the caret 1000 times, it will cause 1000 writes to be made to settings.json.

There is no problem in terms of speed, so if you don't care about the storage load, you can use it without any problem. But it's best not to do it.

An alternative is to set up a server and communicate with AutoHotkey. This will allow you to get the caret location without any problems.
However, it is too difficult to write the communication process in AutoHotkey, so would have to create a library, but then it is faster to use Acc from the start.

postscript:
I noticed that the window title can be changed using AutoHotkey. This solves the above problem.
zero-plusplus
Posts: 26
Joined: 23 May 2020, 08:01
Contact:

Re: new vscode extension allows advanced AHK V1/V2 debugging

07 Jan 2021, 11:42

Thank you for your patience.
The planned extension has been merged into another extension.

Since the URL does not display correctly, please search for "zero-plusplus.vscode-operate-from-autohotkey" in the VSCode Marketplace.

This extension is for executing commands that do not have a shortcut key set. It also includes commands to get some context information (such as caret position).
See the README for details.

Return to “Visual Studio Code”

Who is online

Users browsing this forum: No registered users and 6 guests