ListBox Enter pressed event?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
slishnevsky
Posts: 27
Joined: 07 Mar 2024, 06:50

ListBox Enter pressed event?

Post by slishnevsky » 15 Apr 2024, 13:31

Hello.

Is there an Enter pressed event for ListBox in AHK 2?

Let's say I selected an item in ListBox, and I want to submit it by pressing Enter key?

Is it possible, like using OnEvent() method for example?

Something like

Code: Select all

MyListBox.OnEvent('Enter', EnterPressed)
I know there is no "Enter" event for ListBox, so, what should I use instead?

"Change" event exists, but it doesn't capture Enter press.

Thanks

User avatar
boiler
Posts: 16997
Joined: 21 Dec 2014, 02:44

Re: ListBox Enter pressed event?

Post by boiler » 15 Apr 2024, 14:14

You can do it with an invisible button that is set to Default:

Code: Select all

#Requires AutoHotkey v2.0
MyGui := Gui()
MyGui.Add('ListBox', 'vColors', ['Red','Green','Blue'])
MyGui.Add('Button', 'y-30 Default').OnEvent('Click', EnterPressed)
MyGui.Show

EnterPressed(*) {
	MsgBox MyGui['Colors'].Text ' was chosen!'
}

XMCQCX
Posts: 231
Joined: 14 Oct 2020, 23:44

Re: ListBox Enter pressed event?

Post by XMCQCX » 16 Apr 2024, 14:38

Hotkeys can be assigned for when the GUI is active using HotIfWinActive.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

gMainTitle := 'GuiTitle - ScriptName'

HotIfWinActive(gMainTitle ' ahk_class AutoHotkeyGUI')
Hotkey('Enter', gMain_lb_Submit)

gMain := Gui(, gMainTitle)
gMain.lb := gMain.Add('ListBox', 'w300 h300', ['Red','Green','Blue'])
gMain.Show()

gMain_lb_Submit(*)
{
    try strTxt := ControlGetChoice(gMain.lb.hwnd, gMainTitle ' ahk_class AutoHotkeyGUI')
    catch
        return
   
    ToolTip('Submit: ' strTxt), SetTimer((*) => ToolTip(), -6000)   
}

ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: ListBox Enter pressed event?

Post by ntepa » 16 Apr 2024, 16:12

Another option:

Code: Select all

#Requires AutoHotkey v2.0

MyGui := Gui()
MyListBox := MyGui.AddListBox(, ["red","green","blue"])
WM_KEYDOWN := 0x0100
OnMessage(WM_KEYDOWN, OnKeyDown)
MyGui.Show()

OnKeyDown(wParam, lParam, msg, hwnd) {
    key := GetKeyName(Format("VK{:x}", wParam))
    if hwnd = MyListBox.hwnd && key = "Enter" {
        MsgBox(MyListBox.Text " selected")
    }
}

Danielsan73
Posts: 21
Joined: 07 Nov 2014, 10:20

Re: ListBox Enter pressed event?

Post by Danielsan73 » 17 Apr 2024, 08:47

Got same problem: i can't store my variable "Etichetta" out of ok_click funtion:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

Spessore := 1
Cliente := "Codice/Cliente"
Posiz := 1
Altezza := 10
Qta := 1
Mat := "Fe."
Qual := "S235JR"
Etichetta := ""

MyGui := Gui(,"Etichettatrice")
MyGui.Add("Text",, "Inserisci i dati per la compilazione Automatica       ")
MyGui.Add("Text",, "Spessore in mm",)
MyGui.Add("ComboBox", "vSpessore w150", ["1","1.5","2","2.5","3","4","5","6","8","10","12","15","20","25","30","35"])
MyGui.Add("Text",, "Cliente o Codice")
MyGui.AddEdit("vCliente w150",Cliente)
MyGui.Add("Text",, "Progressivo Posizione")
MyGui.AddEdit("vPosiz w150",Posiz)
MyGui.Add("Text",, "Altezza del testo")
MyGui.AddEdit("vAltezza w150",Altezza)
MyGui.Add("Text",, "Quantità")
MyGui.AddEdit("vQta w150",Qta)
MyGui.Add("Text",, "Materiale")
MyGui.Add("ComboBox", "vMat w150", ["Fe.","Hardox.","inox.","Domex.","Zincata.","Bugnata.","Striata.","Alu.","Ottone."])
MyGui.Add("Text",, "Qualità")
MyGui.Add("ComboBox","vQual w150",["S235JR","S275JR","S355J2","S235JR Decapata","H450","H500","Aisi304","Aisi304 Satinata","Aisi3016"])
Etichetta := MyGui.Add("Button", "Default w150", "OK").OnEvent("Click", OK_Click)
MyGui.Show
WinSetAlwaysOnTop "1", "Etichettatrice"
ControlChooseIndex "1", "ComboBox1","Etichettatrice"
ControlChooseIndex "1", "ComboBox2","Etichettatrice"
ControlChooseIndex "1", "ComboBox3","Etichettatrice"

OK_Click(*)
{
Salvati := MyGui.Submit()  ; Save the contents of named controls into an object
    MyGui.Hide()
	Etichetta := Salvati.Spessore . "mm-" . Salvati.Cliente . "." . Salvati.Posiz . "-" . Salvati.Qta . "pz " . Salvati.Qual
	tooltip Etichetta
	msgbox Etichetta
	Return Etichetta
}


F10::
{
MyGui.Show
}


F9::
{
MsgBox Etichetta
}
it work inside but when press F9 the variable is empty :( helpme


[Mod edit: Changed inline code tags (c) to [code][/code] tags that produce an AHK code box. Please use them yourself when posting code.]

User avatar
boiler
Posts: 16997
Joined: 21 Dec 2014, 02:44

Re: ListBox Enter pressed event?

Post by boiler » 17 Apr 2024, 09:23

It can't do the same things as the other function if you don't tell it to do those same things. Even easier, just call the other function:

Code: Select all

F9::OK_Click

Danielsan73
Posts: 21
Joined: 07 Nov 2014, 10:20

Re: ListBox Enter pressed event?

Post by Danielsan73 » 17 Apr 2024, 09:47

ok but call the function not store my variable i need it to menage in other functions,
successive step is how to menage variables in the gui increase, subtract, change combobox etc etc by a key and store it to " send %etechetta% " like in v1 AutoHotKey .

User avatar
boiler
Posts: 16997
Joined: 21 Dec 2014, 02:44

Re: ListBox Enter pressed event?

Post by boiler » 17 Apr 2024, 10:12

If you want what you assign inside the function (like the Etichetta variable) to be seen outside the function, then you need to declare it as global in the function and any other function in which you want to access it that assigns it a new value since it will assume it's local in those cases. In this version of the code, after you submit the GUI by clicking OK, the MsgBox will display the contents of Etichetta when you press F9 because it is declared global in the function that assigns its value.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

Spessore := 1
Cliente := "Codice/Cliente"
Posiz := 1
Altezza := 10
Qta := 1
Mat := "Fe."
Qual := "S235JR"
Etichetta := ""

MyGui := Gui(,"Etichettatrice")
MyGui.Add("Text",, "Inserisci i dati per la compilazione Automatica       ")
MyGui.Add("Text",, "Spessore in mm",)
MyGui.Add("ComboBox", "vSpessore w150", ["1","1.5","2","2.5","3","4","5","6","8","10","12","15","20","25","30","35"])
MyGui.Add("Text",, "Cliente o Codice")
MyGui.AddEdit("vCliente w150",Cliente)
MyGui.Add("Text",, "Progressivo Posizione")
MyGui.AddEdit("vPosiz w150",Posiz)
MyGui.Add("Text",, "Altezza del testo")
MyGui.AddEdit("vAltezza w150",Altezza)
MyGui.Add("Text",, "Quantità")
MyGui.AddEdit("vQta w150",Qta)
MyGui.Add("Text",, "Materiale")
MyGui.Add("ComboBox", "vMat w150", ["Fe.","Hardox.","inox.","Domex.","Zincata.","Bugnata.","Striata.","Alu.","Ottone."])
MyGui.Add("Text",, "Qualità")
MyGui.Add("ComboBox","vQual w150",["S235JR","S275JR","S355J2","S235JR Decapata","H450","H500","Aisi304","Aisi304 Satinata","Aisi3016"])
Etichetta := MyGui.Add("Button", "Default w150", "OK").OnEvent("Click", OK_Click)
MyGui.Show
WinSetAlwaysOnTop "1", "Etichettatrice"
ControlChooseIndex "1", "ComboBox1","Etichettatrice"
ControlChooseIndex "1", "ComboBox2","Etichettatrice"
ControlChooseIndex "1", "ComboBox3","Etichettatrice"

OK_Click(*)
{
Salvati := MyGui.Submit()  ; Save the contents of named controls into an object
    MyGui.Hide()
	global Etichetta := Salvati.Spessore . "mm-" . Salvati.Cliente . "." . Salvati.Posiz . "-" . Salvati.Qta . "pz " . Salvati.Qual
	tooltip Etichetta
	msgbox Etichetta
	Return Etichetta
}

F10::MyGui.Show

F9::MsgBox Etichetta

Danielsan73
Posts: 21
Joined: 07 Nov 2014, 10:20

Re: ListBox Enter pressed event?

Post by Danielsan73 » 17 Apr 2024, 10:56

Thankyou boiler

that i need ! solved

Post Reply

Return to “Ask for Help (v2)”