trigger listbox via enter and doubleclick Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
jekko1976
Posts: 63
Joined: 10 Oct 2014, 07:03

trigger listbox via enter and doubleclick

05 Aug 2020, 04:02

Hello,

the code below trigger an element into a listbox when i doubleclick it.

I want to modify it in order to react either if doubleclick it or if i go over it with the arrows and press "Enter". It have to work in both ways.

Any ideas?
Thank you so much

Code: Select all

Gui, font, s10, Arial
Gui, Add, ListBox, x10 y10 w350 h600 vMyScript gLBox, %lista%
Gui, Show
Return

GuiClose:
GUI, destroy
ExitApp
return

LBox:
SetCapsLockState, Off
If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
Return	;Do nothing.
Gui, Submit, NoHide ;Submit the data so we can see the value of Servizio.
If(!MyScript)	;If there is no value (if nothing has been selected on double click)
Return	;Do nothing.
Else	;If there is a value
...
...
...
User avatar
boiler
Posts: 6586
Joined: 21 Dec 2014, 02:44

Re: trigger listbox via enter and doubleclick  Topic is solved

05 Aug 2020, 05:55

You can make Enter a hotkey that's active only when the GUI window is active:

Code: Select all

lista := "eenie|meenie|miney|mo"
Gui, +HwndGuiHwnd
Gui, font, s10, Arial
Gui, Add, ListBox, x10 y10 w350 h600 vMyScript gLBox, %lista%
Gui, Show
Return

#If WinActive("ahk_id" GuiHwnd)
Enter::
NumpadEnter::
	Gui, Submit, NoHide
	MsgBox, % "Selected: " MyScript
return
#If

GuiClose:
GUI, destroy
ExitApp
return

LBox:
	SetCapsLockState, Off
	If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
		Return	;Do nothing.
	Gui, Submit, NoHide ;Submit the data so we can see the value of Servizio.
	If(!MyScript)	;If there is no value (if nothing has been selected on double click)
		Return	;Do nothing.
	Else	;If there is a value
	Gui, Submit, NoHide
	MsgBox, % "Selected: " MyScript
return
jekko1976
Posts: 63
Joined: 10 Oct 2014, 07:03

Re: trigger listbox via enter and doubleclick

05 Aug 2020, 09:24

Thank you so much! Brillant idea!

There has been another problem but i managed to resolve it and i am proud of myself :lol:

i modified the code in the way below, because when i was pressing "Enter" the flow was jumping before the (A_GuiControlEvent != "DoubleClick"), so after the "Enter" the script keeps waiting for the doubleclick aswell.

So in the hotkey i told the script to jump after the (A_GuiControlEvent != "DoubleClick") and it works like a charm

thank you so much for the solution!

Code: Select all

Gui, +HwndGuiHwnd
Gui, font, s10, Arial
Gui, Add, ListBox, x10 y10 w350 h600 vMyScript gLBox, %lista%
Gui, Show
Return

#If WinActive("ahk_id" GuiHwnd)
Enter::
Gui, Submit, NoHide
goto Lbox1
#If

GuiClose:
GUI, destroy
ExitApp
return

LBox:
SetCapsLockState, Off
If (A_GuiControlEvent != "DoubleClick") ;If user didn't double click the listbox
Return	;Do nothing.
Lbox1:
Gui, Submit, NoHide ;Submit the data so we can see the value of Servizio.
If(!MyScript)	;If there is no value (if nothing has been selected on double click)
Return	;Do nothing.
Else	;If there is a value
...
Last edited by jekko1976 on 05 Aug 2020, 10:46, edited 1 time in total.
User avatar
boiler
Posts: 6586
Joined: 21 Dec 2014, 02:44

Re: trigger listbox via enter and doubleclick

05 Aug 2020, 10:24

I don’t understand what the issue was. The script as I posted it worked without problems, either with Enter or double-click. Also, using Goto should generally be avoided. There is almost always a better solution.
jekko1976
Posts: 63
Joined: 10 Oct 2014, 07:03

Re: trigger listbox via enter and doubleclick

05 Aug 2020, 10:45

boiler wrote:
05 Aug 2020, 10:24
I don’t understand what the issue was. The script as I posted it worked without problems, either with Enter or double-click. Also, using Goto should generally be avoided. There is almost always a better solution.
Dear boiler,

i don't know why, but when i added the lines that you suggested to my script this happened:

when i was pressing "Enter" in the item of my choose, the script correctly reported
Selected: MyItem
and this was ok.
But then, the script keeps waiting for a doubleclick to proceed to the rest of the istructions (in short words, after the last "Else"). It didn't go directly to the commands of the rest of the script.

So as a workaround, when the new hotkey "Enter" was pressed I sent the script via goto after the A_guiControlEvent

I am sorry for my bad english and my worst coding skills :D
Obviously the only thing of my interest is that now my problem is resolved and i thank you again for the idea that you gave me.
User avatar
boiler
Posts: 6586
Joined: 21 Dec 2014, 02:44

Re: trigger listbox via enter and doubleclick

05 Aug 2020, 13:46

Rather than using Goto, it would be better if you had two triggering routines (the Enter key and double-click) that both call a separate subroutine with the code you want executed using Gosub. Like this:

Code: Select all

Gui, +HwndGuiHwnd
Gui, font, s10, Arial
Gui, Add, ListBox, x10 y10 w350 h600 vMyScript gLBox, %lista%
Gui, Show
Return

#If WinActive("ahk_id" GuiHwnd)
Enter::Gosub, LBox1
#If

GuiClose:
GUI, destroy
ExitApp
return

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

Lbox1:
	Gui, Submit, NoHide ;Submit the data so we can see the value of Servizio.
	If(!MyScript)	;If there is no value (if nothing has been selected on double click)
		Return	;Do nothing.
	; you do not need an Else here because it only gets to this point If there is a value
	MsgBox, this is where your code goes
return
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: trigger listbox via enter and doubleclick

05 Aug 2020, 19:08

Hi,

I might mention that, in future cases where you will have more than one focus-capable control in your GUI, you can take advantage of GuiControlGet or of the following (cryptic admittedly):

Code: Select all

; ...
#If (DllCall("User32.dll\GetFocus", "Ptr") = hListBox) ; hListBox should contains the hwnd of your listbox
; ...
to make GUI control-specific hotkeys (see also: Control-specific hotkeys!).

A_AhkUser
my scripts

Return to “Ask For Help”

Who is online

Users browsing this forum: ArkuS, codude, Google [Bot], mikeyww, Renegaduh, Xtra and 37 guests