First Script, Several Questions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
voidedalteration
Posts: 3
Joined: 22 Aug 2017, 00:18

First Script, Several Questions

Post by voidedalteration » 31 Aug 2017, 05:36

I'm writing a script to assist me in operating a program via a touch screen, and offer a degree of automation for an annoying task.

It works, but not like I intend and isnt very reliable.
I created hotkeys to perform all of the functions I would like, as well as a box in the top right to act as an idicator of which mode of operation it is in.
it should ideally work as follows
upon pressing the "AUTO" gui button, it changes the top box to reflect the auto state and then it begins to search for a color, when it finds that color, it peforms a controlmouseclick and a few keystrokes, then begins to check the color location's Y coordinate against the previous coordinate, if theres a difference, it performs mouse and the keystroke again.
I have this working, to a degree, but it seems like whenever the pixel is found around at Y300px in screen, or relative coordmode. it loses control of the active window, like the controlclick is clicking in the wrong coord? and this is usually where it gets weird and i have to restart the script.

something I would like but havent figured out how to do reliably yet, is upon pressing the "MANUAL" gui button, the script ceases the pxsearch loop and performs a keystroke (switching windows within the program) and allows the user to operate the gui manually via the UP/DOWN and ENTER Gui buttons, and reflect this state in the upper right box. Upon pressing either Manual again, or AUTO, it resumes the pixel search.

In the beginning I had it automatically scrolling the window until the pixel was found at a high enough point in the screen, but that proved to just give me a headache so i abandoned it. any pointers towards achieving it effectively would be appreciated.

here's the code:

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen
Gui, 1:+AlwaysOnTop +ToolWindow -Caption +Border +E0x08000000   ;WS_EX_NOACTIVATE E0x08000000
Gui, 1:font, s15, Verdana
Gui, 1:font, w800
Gui, 1:font, cRed
Gui, 1:Add, Edit, vmyEdit w90 h33 -VScroll +Center +Uppercase +Limit6 +ReadOnly
Gui, 1:Show , x1000
Gui, 1:Show , y15
Gui, 1:Show, noactivate,Test
Gui, 2:+AlwaysOnTop +ToolWindow -Caption +Border +E0x08000000   ;WS_EX_NOACTIVATE E0x08000000
Gui, 2:font, s18
Gui, 2:font, wDefault
Gui, 2:Add, Button, gesc w120 h70, EXIT
Gui, 2:Add, Button, g^Up w120 h70 xp120, UP
Gui, 2:Add, Button, g^Down w120 h70 xp120, DOWN
Gui, 2:Add, Button, genter w120 h70 xp120, ENTER
Gui, 2:Add, Button, g!f10 w120 h70 xp120, MANUAL
Gui, 2:Add, Button, g!f11 w220 h70 xp120, AUTO
Gui, 2:Show , x500
Gui, 2:Show , y775
Gui, 2:Show, noactivate,Test
return

^!r::Reload 
return

enter::
sleep, 10
Send, ^{Left}
Send, {Enter}
return

!f10:: 
IfWinNotActive, ENPS
{
    WinActivate, ENPS	; activates enps
}
if (clip <> "MAN") 
	{
		send, !{Left}
		ClipSave := ClipboardAll ; store the current clipboard content
		Clipboard := "MAN" ; clear the clipboard
		sleep, 200
		clip := Clipboard
		GuiControl, 1:, myEdit, %clip%
		Clipboard := ClipSave
		sleep 5000 ; failsafe
	}	
if (clip = "MAN") 
	{
		send, !{Left}
		ClipSave := ClipboardAll ; store the current clipboard content
		Clipboard := "AUTO" ; clear the clipboard
		sleep, 200
		clip := Clipboard
		GuiControl, 1:, myEdit, %clip%
		Clipboard := ClipSave
		sleep 5000 ; failsafe
	}		
return

^Down::
IfWinNotActive, ENPS
{
    WinActivate, ENPS	; activates enps
}
IfWinActive , ENPS
{
Send, {WheelDown}
sleep, 100
}
return


^Up::
IfWinNotActive, ENPS
{
    WinActivate, ENPS
}
IfWinActive, ENPS
{
Send, {Wheelup}
sleep, 100
}
return

!f11::
Auto := "1"
ClipSave := ClipboardAll ; store the current clipboard content
Clipboard := "AUTO" ; clear the clipboard
sleep 200 
clip := Clipboard
Main:
GuiControl, 1:, myEdit, %clip%
WinActivate, ENPS
	Loop, 
		{
		msgbox, %auto%
		WinActivate, ENPS
		PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
		sleep, 500
			sleep, 10
			CoordMode, Pixel, Window
			ControlClick, x%Px% y%Py%, ENPS ;click ENPS
			sleep, 500
			send, ^{Left}
			send, {enter}
			sleep, 10
			Vy := Py
			msgbox, %vy% is %py%
			sleep, 1000
			PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
			sleep, 500
			if (%Vy% <> %Py%)
				{
				send, ^{Left}
				send, {enter}
				}
			break
			}
	sleep, 200
msgbox, %vy% is not %py%
goto, Main
return

esc::
ExitApp

Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: First Script, Several Questions

Post by Nightwolf85 » 31 Aug 2017, 10:56

I have no way of testing your code, but looking through it the best guess I have is that you are using CoordMode, Pixel, Screen but reading how ControlClick works, with the method you are using, it is relative to the window not the screen, sounds like that might be the problem.
Documentation wrote:Mode 1 (Position): Specify the X and Y coordinates relative to the target window's upper left corner. The X coordinate must precede the Y coordinate and there must be at least one space or tab between them. For example: X55 Y33. If there is a control at the specified coordinates, it will be sent the click-event at those exact coordinates. If there is no control, the target window itself will be sent the event (which might have no effect depending on the nature of the window). Note: In this mode, the X and Y option letters of the Options parameter are ignored.
Edit: what is the purpose of doing this:

Code: Select all

ClipSave := ClipboardAll ; store the current clipboard content
Clipboard := "AUTO" ; clear the clipboard
sleep 200 
clip := Clipboard
Why don't you just set clip to the desired value?

Edit2: It appears your Loop will always execute your break at the end, so why is it in a loop? is the break supposed to be inside the IF? I guess the longer I look at the code the less I know what is being attempted. I even noticed you have a CoordMode, Pixel, Window inside the loop, but after the first pixel search.

Edit3: Complete shot in the dark, but I tried to guess at what was going on:

Code: Select all

#SingleInstance Force
SetBatchLines, -1

CoordMode, Pixel, Window
Gui, 1:+AlwaysOnTop +ToolWindow -Caption +Border +E0x08000000   ;WS_EX_NOACTIVATE E0x08000000
Gui, 1:font, s15, Verdana
Gui, 1:font, w800
Gui, 1:font, cRed
Gui, 1:Add, Edit, vmyEdit w90 h33 -VScroll +Center +Uppercase +Limit6 +ReadOnly
Gui, 1:Show , x1000
Gui, 1:Show , y15
Gui, 1:Show, noactivate,Test
Gui, 2:+AlwaysOnTop +ToolWindow -Caption +Border +E0x08000000   ;WS_EX_NOACTIVATE E0x08000000
Gui, 2:font, s18
Gui, 2:font, wDefault
Gui, 2:Add, Button, gdoExit w120 h70, EXIT
Gui, 2:Add, Button, gdoUp w120 h70 xp120, UP
Gui, 2:Add, Button, gdoDown w120 h70 xp120, DOWN
Gui, 2:Add, Button, gdoEnter w120 h70 xp120, ENTER
Gui, 2:Add, Button, gstartManual w120 h70 xp120, MANUAL
Gui, 2:Add, Button, gstartAuto w220 h70 xp120, AUTO
Gui, 2:Show , x500
Gui, 2:Show , y775
Gui, 2:Show, noactivate,Test
return

^!r::Reload

$Enter::
doEnter:
Send, ^{Left}
Send, {Enter}
return

!F10::
startManual:
IfWinNotActive, ENPS
    WinActivate, ENPS	; activates enps
IF (Auto) {
	send, !{Left}
	Auto := False
	SetTimer, Main, Off
	GuiControl, 1:, myEdit, MAN
}	
Else {
	send, !{Left}
	GoSub, startAuto
}		
Return

^Down::
doDown:
IfWinNotActive, ENPS
    WinActivate, ENPS	; activates enps
IfWinActive, ENPS
	Send, {WheelDown}
return


^Up::
doUp:
IfWinNotActive, ENPS
    WinActivate, ENPS
IfWinActive, ENPS
	Send, {Wheelup}
Return


!F11::
startAuto:
Auto := True
SetTimer, Main, 100
GuiControl, 1:, myEdit, AUTO
Return

Main:
	WinActivate, ENPS
	PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
	ControlClick, x%Px% y%Py%, ENPS ;click ENPS
	send, ^{Left}
	send, {Enter}
	Vy := Py
	PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
	IF (%Vy% <> %Py%) {
		send, ^{Left}
		send, {Enter}
	}
Return

Esc::
doExit:
ExitApp

voidedalteration
Posts: 3
Joined: 22 Aug 2017, 00:18

Re: First Script, Several Questions

Post by voidedalteration » 31 Aug 2017, 14:37

thanks for the tip on coordmode, I didnt realize that controlclick would be relative to window regardless, i'll try and see if i can get it working right again today.

as for you question regarding the clip, lol, that was just some copy/pasted code i found and hastily threw together, i should just set clip to desired value, as suggested. haha.
and the break for the loop was in the complete wrong place in the version of the script i posted for you to look at, my apologies, I was also working on trying to figure out why i was getting the VAR is not VAR msg box on every run, lololol.... the revised snippet looks like this

going to try your tips today, thanks again for the help.

Code: Select all

	Loop, 
		{
		msgbox, %auto%
		WinActivate, ENPS
		CoordMode, Pixel, Screen
		PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
		sleep, 500
			sleep, 10
			CoordMode, Pixel, Screen
			ControlClick, x%Px% y%Py%, ENPS ;click ENPS
			sleep, 500
			send, ^{Left}
			send, {enter}
			sleep, 10
			Vy = %Py%
			msgbox, %vy% is %py%
			sleep, 1000 
			PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
			sleep, 500
			if (Vy <> Py)
				{
				ControlClick, x%Px% y%Py%, ENPS ;click ENPS	
				send, ^{Left}
				send, {enter}
				break
				}
			}
	sleep, 200
msgbox, %vy% is not %py%
PixelSearch, Px, Py, 30, 100, 900, 900, 0xFFFF00, 0, FAST, RGB
if Errorlevel = 0
goto, Main
return

voidedalteration
Posts: 3
Joined: 22 Aug 2017, 00:18

Re: First Script, Several Questions

Post by voidedalteration » 31 Aug 2017, 23:22

again, thank you Nightwolf85, I know that code was a mess, Your suggestions worked great, I was able to take your rewrite and implement it into a new version of the script, that autoscrolls the window reliably, and enters/exits manual mode reliably.
Could you perhaps point me to the documentation for AHK that covers how you set up the AUTO/MAN parameter? I noticed you had a timer set and im assuming the IF (auto) means if auto evaluates to true?
sorry I'm very new to this and scripting has never been my strong suit.

I'm still having the issue with Pixelsearch and ControlClick,
even after assigning control click the NA and delay to make it more reliable...no effect. I also switched all pixelsearch coordmodes to relative and made sure it was activating the correct window via ahk_id.

it seems there is an 8px discrepancy in the relative values returned for pixelsearch and control click, but not all the time, so it works fine until the yellow bar its searching for is less than 300px relative to the top of the window, it then returns a blank value, which befuddles the control click and it clicks god knows where. I'm going to try changing it from control click to mouse click on the morrow and see how that works out.

again, thanks a lot for your help. I know the code was a mess!

Post Reply

Return to “Ask for Help (v1)”