PabloG
PabloG

Reputation: 26735

How can an AutoHotkey GUI handle the {Return} key without using a default button?

I'm using a AutoHotkey GUI + ListBox to select an option from various choices, but I cannot find a way to make the RETURN key to close the GUI without creating an additional default button with an associated ButtonOk event. Here's my code using a default button:

Gui, +LastFound +AlwaysOnTop -Caption   
Gui, Add, ListBox, vMyListBox gMyListBox w300 r10
Gui, Add, Button, Default, OK

GuiControl,, MyListBox, Option 1
GuiControl,, MyListBox, Option 2
GuiControl,, MyListBox, Option 3

Gui, Show
return

MyListBox:
if A_GuiEvent <> DoubleClick
    return

ButtonOK:
    GuiControlGet, MyListBox
    Gui Hide
    MsgBox %MyListBox% selected!
    ExitApp

GuiClose:
GuiEscape:
ExitApp

Upvotes: 1

Views: 2912

Answers (2)

Tom
Tom

Reputation: 7586

You can use the Hotkey command to temporarily enable Return as a hotkey when the GUI is visible and disable it when the gui is closed.

Upvotes: 2

Lexikos
Lexikos

Reputation: 1067

You can monitor for the WM_KEYDOWN message. In the auto-execute section:

OnMessage(0x100, "OnKeyDown")

Then elsewhere in the script:

OnKeyDown(wParam)
{
    if (A_Gui = 1 && wParam = 13) ; VK_ENTER := 13
    {
        GuiControlGet, MyListBox
        Gui Hide
        MsgBox %MyListBox% selected!
        ExitApp
    }
}

Upvotes: 1

Related Questions