Here is a code:
Version 1 (with Gosub):
return
StartGui:
Gui, Add, Edit, x22 y19 w240 h120 vMyEdit, Here is default text
Gui, Add, Button, x22 y179 w100 h30 gGuiCloseOk, Ok
Gui, Add, Button, x162 y179 w100 h30 gGuiCloseCancel, Cancel
; Generated using SmartGUI Creator for SciTE
Gui, Show, w286 h231, My Gui Name
WinGetPos,,, Width, Height, My Gui Name
WinMove, My Gui Name,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
return
return
GuiCloseOK:
GuiControlGet, MyEdit
Gui, Destroy
return
GuiCloseCancel:
MyEdit:=""
Gui, Destroy
return
return
Esc::
Gui, Destroy
return
Place that code in your script anywhere you like. To call for the GUI add anywhere you want Gosub, StartGui
. The content of Edit control you will get in MyEdit
variable.
For example, if you want to call GUI by CTRL+ALT+z put that code in script anywhere you want:
return
!^z::
Gosub, StartGui
return
Version 2 (with function):
GuiFunc(DefaultText)
{
global MyEdit
MyEdit:=""
Gui, Add, Edit, x22 y19 w240 h120 vMyEdit, %DefaultText%
Gui, Add, Button, x22 y179 w100 h30 gGuiCloseOk, &Ok
Gui, Add, Button, x162 y179 w100 h30 gGuiCloseCancel, &Cancel
; Generated using SmartGUI Creator for SciTE
Gui, Show, w286 h231, My Gui Name
WinGetPos,,, Width, Height, My Gui Name
WinMove, My Gui Name,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
return
return
GuiCloseOK:
GuiControlGet, MyEdit
Gui, Destroy
return
GuiCloseCancel:
MyEdit:=""
Gui, Destroy
return
}
return
Esc::
Gui, Destroy
return
Place that code in your script anywhere you like. To call for the GUI, call GuiFunc(DefaultText)
function with parameter that is text to display in Edit control by default. After running function global variable MyEdit
is set to the content of Edit control. So you can use MyEdit
variable anywhere outside the function. I know that you wanted function to return content of MyEdit
variable but I tried it many ways with no success.
For example, put that code in your script anywhere you want. To call GUI press CTRL+ALT+z and to display content of MyEdit
variable press CTRL+ALT+a :
return
!^z::
DefaultText:= "Here is default text"
GuiFunc(DefaultText)
return
return
!^a::
MsgBox, %MyEdit%
return
For both versions, if you decide to rename Window Title My Gui Name
then keep in mind that you need to rename it in 3 places for script to work properly.
Also, always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!