Saturday, July 21, 2018

autohotkey - AHK: How to use a Hotkey in conjunction with GUI



My GUI works fine.




Here is a screenshot of my Edit box, GUI:



enter image description here



F1 fires the script.
I would then input 1, 2, 3, or 4, and Click Submit or hit ENTER; which Enter doesn't currently get accepted but sends it into a hidden mode or something unbeknownst to me that's happening in AHK world. -- fixed by davidmneedham -- thanks!



code: not working as fully desired



#NoEnv
#SingleInstance Force


F1::

aa := "1) opt 1 or (f)"
bb := "2) opt 2 (v)"
cc := "3) open opt 3"
dd := "4) open opt 4"

Gui, Add, Text, x50 y50 w100 h30, %aa%
Gui, Add, Text, x50 y70 w100 h30, %bb%

Gui, Add, Text, x50 y90 w300 h30, %cc%
Gui, Add, Text, x50 y110 w300 h30, %dd%

Gui, Add, Text, x50 y140 w50 h20, Selection:
Gui, Add, Edit, x100 y140 w100 h20 vChoice
Gui, Add, Text, x205 y140 w300 h30, (press ENTER)


;Gui, Add, Button, x50 y170 w50 h30 default gCancel, Cancel
;Gui, Add, Button, x130 y170 w50 h30 gSubmit, Submit


;Enter command fix by davidmneedham on StackExchangs thanks!
Gui, Add, Button, x50 y170 w50 h30 gCancel, Cancel
Gui, Add, Button, x130 y170 w50 h30 default gSubmit, Submit

Gui, Color, EEAA99
Gui +LastFound ; Make the GUI window the last found window for use by the line below.
WinSet, TransColor, ff00ff
Gui, Show, w350 h250, Enter Selection


;I even tried a while loop, here but it caused other problems
;while(true)
;{
; If (GetKeyState("f"))
; {
; msgbox, f pressed
; break
; }
;}


return


Submit:
Gui, Submit

if (Choice = "2")
{
msgbox chose 2
}

else if (Choice = "3")
{
msgbox chose 3
}
else if (Choice = "4")
{
msgbox chose 4
}
else
{

msgbox chose 1
}

ButtonCancel:
Gui, destroy
return

;One suggestion I tried this

#If WinActive("Download ahk_exe AutoHotkey.exe")

f:: Send 2{Tab 2}{Enter}
v:: Send 3{Tab 2}{Enter}
#If


What I am trying to incorporate is this:
F1, ENTER 1,2,3,4
OR plainly
press f to fire "2, ENTER"
press v to fire "3, ENTER"

I've looked at the this code from (LINK CODE HERE_HOTKEYS) and looking at this (LINK CODE HERE_KEYPRESS):



Investigating code:



#SingleInstance force


Input, Key, L1

MsgBox You pressed %Key%!

OnExit ExitSub
return

ExitSub:
ExitApp



I can't seem how to incorporate this into
F1 firing the script and accepting the
GUI original code OR accept f or v, in
which any of the line will end the script
and will not run again until F1 if pressed.




Recap:


F1 fires script
2, Enter
or
3, Enter
or
4, Enter
or
f
or

v
... ends the script until F1 pressed once more.

Answer



The reason that your window closes without any action when pressing Enter is that the Cancel button is listed as your default action.



Change these lines:



Gui, Add, Button, x50  y170 w50 h30  default gCancel, Cancel 
Gui, Add, Button, x130 y170 w50 h30 gSubmit, Submit



And make the Submit button the default action:



Gui, Add, Button, x50  y170 w50 h30  gCancel, Cancel 
Gui, Add, Button, x130 y170 w50 h30 default gSubmit, Submit


The reason your context-sensitive hotkeys do not work is because you have the wrong WinTitle: your window title is "Enter Selection". Use the following lines instead




#If WinActive("Enter Selection")
f:: Send 1{Tab 2}{Enter}
v:: Send 2{Tab 2}{Enter}
#If


The complete functional script is below:



#NoEnv
#SingleInstance force


F1::

aa := "1) opt 1 or (f)"
bb := "2) opt 2 (v)"
cc := "3) open opt 3"
dd := "4) open opt 4"

Gui, Add, Text, x50 y50 w100 h30, %aa%
Gui, Add, Text, x50 y70 w100 h30, %bb%

Gui, Add, Text, x50 y90 w300 h30, %cc%
Gui, Add, Text, x50 y110 w300 h30, %dd%

Gui, Add, Text, x50 y140 w50 h20, Selection:
Gui, Add, Edit, x100 y140 w100 h20 vChoice
Gui, Add, Text, x205 y140 w300 h30, (press ENTER)

Gui, Add, Button, x50 y170 w50 h30 gCancel, Cancel
Gui, Add, Button, x130 y170 w50 h30 default gSubmit, Submit


Gui, Color, EEAA99
WinSet, TransColor, ff00ff
Gui, Show, w350 h250, Enter Selection

return

#If WinActive("Enter Selection")
f:: Send 1{Tab 2}{Enter}
v:: Send 2{Tab 2}{Enter}
#If


Submit:
Gui, Submit

if (Choice = "2")
{
msgbox chose 2
}
else if (Choice = "3")
{

msgbox chose 3
}
else if (Choice = "4")
{
msgbox chose 4
}
else
{
msgbox chose 1
}


Gui, Destroy
return

Cancel:
Gui, Destroy
return

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...