List box selection in If code block not being recognized.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gabelynn1
Posts: 3
Joined: 06 Dec 2019, 12:51

List box selection in If code block not being recognized.

02 May 2024, 14:58

I have very little coding knowledge, but I write scripts w/ AHK to help speed up tasks at work. This script isn't pretty and could certainly be improved, but it does work. All but one bug that I can't figure out.
I can make listbox selections in any tab and the code is executed, except in my first tab. The only selection that works in the Main tab is the first choice. The rest do not.

Code: Select all

#IfWinActive ahk_class Notepad
~Left & ~Right::
if WinExist("Putty Item Picker") {
        ; If the GUI exists, toggle between Notepad and the GUI
        WinGet, active_id, ID, A
        if (active_id = WinExist("ahk_class Notepad"))
            WinActivate, Putty Item Picker
        else
            WinActivate, ahk_class Notepad
    } else {
Gui, Destroy
Gui, +HwndGuiHwnd
Gui, font, s10, Arial
Gui, Add, Tab3, x0 y0 w247 h215 Border vTabName gTabChanged, Main||EAP|LPR|LSD
Gui, Tab, Main
Gui, Add, ListBox, x3 y25 w240 h190 vMainItemSelected gSendDoubleClickedItemMain,
(
Search Text|
Search Item|
Item Help Info|
Chronicles|
Interface Table|
Next 10 NURs|
Next 5 LABs
)
;GuiControl, Choose, MainItemSelected, 1
Gui,Tab, EAP
Gui, Add, ListBox, x3 y25 w240 h190 vEAPItemSelected gSendDoubleClickedItemEAP,
(
Allowed Frequencies - 10938|
Display Name - 10950|
Display Name Settings - 10921|
EAP Specific Specimen Source - 10700|
Order Composer Config (OCC) - 24210|
IP Composer Buttons - 11115|
IP Order Questions (LQL) - 10305
)
Gui, Tab, LPR
Gui, Add, ListBox, x3 y25 w240 h190 vLPRItemSelected gSendDoubleClickedItemLPR,
(
LPR Item - 11111
)
Gui, Tab, LSD
Gui, Add, ListBox, x3 y25 w240 h190 vLSDItemSelected gSendDoubleClickedItemLSD,
(
LSD Item - 22222
)
WinGetPos, winx, winy, winwidth, winheight, ahk_class Notepad
guiwidth := 247
guiheight := 215
guiy := winy + (winheight - guiheight) // 2
guix := winx + winwidth - guiwidth
Gui, Show, x%guix% y%guiy% w247 h215, Putty Item Picker
Gui, +AlwaysOnTop
Send, {Tab}
sleep, 100
SetTimer, CheckWindowNotActive, 200


#IfWinActive, Putty Item Picker
Left::Send,{Tab}{Left}
Right::Send,{Tab}{Right}
Left & Up::Send,{Tab}
Numpad0::Gosub, SendSelectedListItem
Enter::Gosub, SendSelectedListItem
NumpadEnter::Gosub, SendSelectedListItem

#If
CheckWindowNotActive:
 if (!WinExist("Putty Item Picker"))
    {
        ; Stop the timer if the window doesn't exist
        SetTimer, CheckWindowNotActive, Off
        return
    }

    WinGetActiveTitle, activeWinTitle
    ; Specify the titles of the windows to exclude
    windowTitle1 := "Putty Item Picker"
    windowTitle2 := "Notepad"

    ; Check if the active window is not one of the specified windows
    if (activeWinTitle != windowTitle1 and !InStr(activeWinTitle, windowTitle2))
    {
        ; Do something
		WinSet, AlwaysOnTop, Off, Putty Item Picker
		;WinActivate, %activeWinTitle%
		;Activateonce := true
		return
	}else
		if (InStr(activeWinTitle, windowTitle2) or activeWinTitle == windowTitle1){
			WinSet, AlwaysOnTop, On, Putty Item Picker
			WinGetPos, winx, winy, winwidth, winheight, ahk_class Notepad
			guiwidth := 247
			guiheight := 215
			guiy2 := winy + (winheight - guiheight) // 2
			guix2 := winx + winwidth - guiwidth
			WinMove, Putty Item Picker, , %guix2%, %guiy2%
		}
return

TabChanged:
Gui, Submit, Nohide
If (TabName == "Main"){
GuiControl, Choose, MainItemSelected, 1
GuiControl, Choose, EAPItemSelected, 0
GuiControl, Choose, LPRItemSelected, 0
GuiControl, Choose, LSDItemSelected, 0
GuiControl, Focus, MainItemSelected
}
If (TabName == "EAP"){
GuiControl, Choose, MainItemSelected, 0
GuiControl, Choose, EAPItemSelected, 1
GuiControl, Choose, LPRItemSelected, 0
GuiControl, Choose, LSDItemSelected, 0
GuiControl, Focus, EAPItemSelected
}
If (TabName == "LPR"){
GuiControl, Choose, MainItemSelected, 0
GuiControl, Choose, EAPItemSelected, 0
GuiControl, Choose, LPRItemSelected, 1
GuiControl, Choose, LSDItemSelected, 0
GuiControl, Focus, LPRItemSelected
}
If (TabName == "LSD"){
GuiControl, Choose, MainItemSelected, 0
GuiControl, Choose, EAPItemSelected, 0
GuiControl, Choose, LPRItemSelected, 0
GuiControl, Choose, LSDItemSelected, 1
GuiControl, Focus, LSDItemSelected
}
return


SendDoubleClickedItemEAP:
SetCapsLockState, Off
	If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
		Return	;Do nothing.
	Gosub, SendSelectedListItem
return

SendDoubleClickedItemMain:
SetCapsLockState, Off
	If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
		Return	;Do nothing.
	Gosub, SendSelectedListItem
return

SendDoubleClickedItemLPR:
SetCapsLockState, Off
	If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
		Return	;Do nothing.
	Gosub, SendSelectedListItem
return

SendDoubleClickedItemLSD:
	SetCapsLockState, Off
	If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
		Return	;Do nothing.
	Gosub, SendSelectedListItem
return

; Define a routine to send only the numbers from the selected item in the listbox
SendSelectedListItem:
	WinActivate, ahk_class Notepad ; Activate the Notepad window
	Sleep, 200 ; Add a short delay to ensure activation completes
	Gui, Submit, Nohide ; Submit the GUI content without hiding
	Sleep, 100 ; Add a short delay for stability
	If(!EAPItemSelected&&!LPRItemSelected&&!LSDItemSelected&&!MainItemSelected)
		return

	If(EAPItemSelected){
	RegExMatch(EAPItemSelected, "(\d+)", EAPNumberMatch)
	if(EAPNumbermatch1 != ""){
	Sleep, 150
	SendInput, %EAPNumberMatch1%
	sleep, 250
}}
If(LPRItemSelected){
	RegExMatch(LPRItemSelected, "(\d+)", LPRNumberMatch)
	if(LPRNumbermatch1 != ""){
	Sleep, 150
	SendInput, %LPRNumberMatch1%
	sleep, 250
}}
If(LSDItemSelected){
	RegExMatch(LSDItemSelected, "(\d+)", LSDNumberMatch)
	if(LSDNumbermatch1 != ""){
	Sleep, 150
	SendInput, %LSDNumberMatch1%
	sleep, 250
}}
If(MainItemSelected == "Search Text"){
	Sleep, 150
	Send, SearchText Selected
	sleep, 150
}
If(MainItemSelected == "Search Item"){
	MsgBox, Item
	Sleep, 150
	Send, Item
	sleep, 150
}
If(MainItemSelected = "Item Help Info"){
	Sleep, 150
	Send, Help
	sleep, 150
}
If(MainItemSelected = "Chronicles"){
	Sleep, 150
	Send, Chron
	sleep, 150
}
If(MainItemSelected = "Interface Table"){
	Sleep, 175
	Send, AIF
	Sleep, 100
	Send, 105000001{Enter}
	sleep, 150
}
return

#IfWinNotActive ahk_class Notepad
~Left & ~Right::
WinActivate, ahk_class Notepad
return
}
return
Any help would be appreciated.
Thanks
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: List box selection in If code block not being recognized.

02 May 2024, 16:16

Welcome to this AutoHotkey forum!

If I had this script, I would do the following things.
  1. Eliminate non-essential parts of the script for testing, so that you can focus your testing on the areas you believe are problematic. Shortening and simplifying your script will speed your debugging time significantly.
  2. Check how and where you are using the Return command. If Return is not used to end a section of code such as a subroutine, the script will proceed to the next statement.
  3. Check how and where you are using braces to denote code blocks. A hotkey defined by :: cannot be the target of an If statement or code block.
  4. Follow the script line by line to understand what it does.
  5. Inspect, examine, or log the values of your variables, conditional statements, and function returns.
  6. Eliminate timers, loops, and #If directives so that you can just focus on the core actions that you would like to test.
  7. A MsgBox is a fast and simple way to determine whether a line in your script has been reached, though there are alternatives as well.
  8. Shorten the script until it works. Expanding and re-testing is then fast and straightforward.
If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.

Code: Select all

#Requires AutoHotkey v1.1.33.11
Gui Font, s10
Gui Add, Tab3   , vtab w230, Main|EAP
Gui Add, ListBox, vv1  gShow, A|B|C
Gui Tab, EAP
Gui Add, ListBox, vv2  gShow, D|E|F
Gui Show, x400, Test
Return

Show:
If (A_GuiEvent = "DoubleClick") {
 Gui Submit, NoHide
 Switch tab {
  Case "Main": MsgBox 64, % tab, % v1
  Case "EAP" : MsgBox 64, % tab, % v2
 }
}
Return
"Does not work" does not actually describe anything. It does not explain what happens, and it does not explain what should happen.

If your GUI is active, then Notepad will not also be active at the same time. A maximum of one window can be active at any given time.

Now that you have a working script, you can adjust it and re-test iteratively. When it breaks, you will know exactly why.

Code: Select all

#Requires AutoHotkey v1.1.33.11
For each, tabName in tab := ["Main", "EAP"]
 tabList .= (tabList = "" ? "" : "|") tabName
Gui Font, s10
Gui Add, Tab3   , vtabNum w230 AltSubmit, % tabList
Gui Add, ListBox, vv1     gShow, A|B|C
Gui Tab, EAP
Gui Add, ListBox, vv2     gShow, D|E|F
Gui Show, x400, Test
Return

Show:
If (A_GuiEvent = "DoubleClick") {
 Gui Submit, NoHide
 MsgBox 64, % tab[tabNum], % v%tabNum%
}
Return
gabelynn1
Posts: 3
Joined: 06 Dec 2019, 12:51

Re: List box selection in If code block not being recognized.

02 May 2024, 18:30

You're right, that "does not work" doesn't explain anything. But that is not all that I said. However, I could have been more descriptive and specific .I guess I thought that it would be obvious from looking at the code. All of the code blocks are essentially doing the same thing just matching on different list items selected. Everything about my current code works except for what I was trying to describe. I activate the notepad window prior to sending the text, and all of the other list items send what I want except for the ones below the first item in the main tab.

Thank you for your example code though. Hopefully I can integrate it into what I have. I will also take your advice and break apart my code and test more thoroughly.

But, it would still be nice to know what is wrong with my code as it is currently written if somebody can see something obvious.
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: List box selection in If code block not being recognized.

02 May 2024, 19:34

I noticed line 10.

Code: Select all

} else {
Where does this statement end?
gabelynn1
Posts: 3
Joined: 06 Dec 2019, 12:51

Re: List box selection in If code block not being recognized.

05 May 2024, 16:33

I can see where line 10 that you pointed out is confusing and isn't ideal, but it actually doesn't cause my script to function incorrectly.

I ended up changing this:

Code: Select all

Gui, Add, ListBox, x3 y25 w240 h190 vMainItemSelected gSendDoubleClickedItemMain,
(
Search Text|
Search Item|
Item Help Info|
Chronicles|
Interface Table|
Next 10 NURs|
Next 5 LABs
)
to this:

Code: Select all

Gui, Add, ListBox, x3 y25 w240 h190 vMainItemSelected gSendDoubleClickedItemMain,
(
Search Text|Search Item|Item Help Info|Chronicles|Interface Table|Next 10 NURs|Next 5 LABs
)
and now all of my list choices below the first choice in that main tab are recognized and trigger my desired actions.
I guess you can't hit "return" after the | for some reason? I don't think it messed up my other lists, because I go on to pull out a different variable (just the number) in those.
Thanks for the other example code, though. I'm learning from it.
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: List box selection in If code block not being recognized.

05 May 2024, 18:48

I am not following your answer to the question or why your new ListBox code is better than the older code, but great if you have everything working! :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 100 guests