Closing InputBox via "x" doing something different than Cancel [Solved]

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
canisdibellum
Posts: 47
Joined: 09 Oct 2014, 11:44

Closing InputBox via "x" doing something different than Cancel [Solved]

23 Jun 2016, 20:10

I am asking my users to input some data in an input box or to press the "Cancel" button to do somthing like suppress further messages (I'll be changing the text of the cancel button) HOWEVER if this inputbox is triggered by mistake, I still want them to be able to close it normally with the "X" button and Return

Errorlevel for Close is the same as cancel

I know there's a way to do it with one of the wm_messages (wm_close or wm_destroy maybe?) but
A) I don't know how and Google is failing me
B) I don't know if cancel will trigger the same message. (Or Ok for that matter)

I don't want to make another gui just for this if I can avoid it, my project wound up having so many guis for different things I stopped counting and just started naming them

Oh wise gurus of the AutoHotkey Forums I beg your help, yet again. Thank you in advance
Last edited by canisdibellum on 24 Jun 2016, 00:23, edited 1 time in total.
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Closing InputBox via "x" doing something different than Cancel

23 Jun 2016, 23:49

This appears to work (requires v1.1.20+):

Code: Select all

OnMessage(0x10, Func("InputBoxClose"))
ErrorLevel := "Closed"
InputBox value
OnMessage(0x10, Func("InputBoxClose"), 0)
MsgBox InputBox value: %value%`nErrorLevel: %ErrorLevel%
ExitApp

InputBoxClose(wParam, lParam, msg, hwnd) {
    if WinExist("ahk_class #32770 ahk_id " hwnd) {
        DllCall("EndDialog", "ptr", hwnd, "ptr", 3)
        return 0
    }
}
OnMessage is used to monitor the WM_CLOSE message, which is only received when the user closes the window, not when they click OK or Cancel (pressing Escape is the same as clicking Cancel).

EndDialog causes the system DialogBox function to return 3 (EndDialog's second parameter) to the InputBox command. When the return value of DialogBox is not IDOK (1), IDCANCEL (2) or AHK_TIMEOUT (-2), the InputBox command does not set ErrorLevel. Thus, ErrorLevel retains the initial value set by the script ("Closed").

The WinExist check is not perfect, as it only verifies that the window is a dialog. However, OnMessage is not able to monitor messages that are sent directly to dialogs that are defined by the system (such as FileSelectFile), so it's fairly safe. To be even safer, it is best to unregister the message monitor afterward (as shown above). If you were to show a second InputBox (such as by pressing a hotkey) before closing the first one, this code would affect both InputBoxes. If you need to limit it to a specific InputBox by its prompt or title, you can do that with WinExist.

If you want every instance of InputBox in your script to have this behaviour, you can just call OnMessage once in the auto-execute section. However, ErrorLevel needs to be initialized each time if you rely on its value.

InputBox's OutputVar is not populated unless the IDOK or IDCANCEL command is received, so it is blank if the user closes the window (with this code). If you need the value, you can retrieve it by calling ControlGetText from within InputBoxClose.
canisdibellum
Posts: 47
Joined: 09 Oct 2014, 11:44

Re: Closing InputBox via "x" doing something different than Cancel

24 Jun 2016, 00:19

Awesome....exactly the kind of answer I was looking for thanks a lot!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, peter_ahk, Rauvagol, Spawnova, wineguy and 296 guests