I've tough that it can be a nice way for me to learn and for others to find answers on some basic questions.
If you are new, while reading this post, you should learn a bit about scripting from reading the questions and analyzing code.
Since now I will make it in the way:
- Question/Problem not solved.
- Question/Problem party fixed.
- Question/Problem with solution included in codes.
Table of Contents- Question 1: How can I make my script starting from click on button START and stop after button STOP?
- Question 2: How can I have multiple buttons START/STOP with same titles but with different functions in same GUI?
- Question 3: How can I run two different functions from one GUI, to send different informations to same application at one time.
- Question 4: How can I move my application/script to tray while pressing on close 'X' button from GUI?
- Question 5: How to detect, if animated cursor changed in application?
Thanks to: Leef_me, wooly_sammoth, Z_Gecko
Here are examples of code that will be edited each time when we solve a problem.
1st Code:
Code:
#SingleInstance, off
#SingleInstance, off
Gui, Add, GroupBox, x6 y7 w460 h80 , Hotkeys
Gui, Add, Text, x16 y27 w70 h20 , Select Hotkey:
Gui, Add, DropDownList, x96 y27 w70 h180 vH, F1|F2|A||B|C|D|E|F
; 'vH' Assigns selected value to variable %H%.
Gui, Add, Text, x176 y27 w90 h20 , Select Timing [ms]:
Gui, Add, ComboBox, x276 y27 w60 h180 vT, 1000||2000|3000|4000|5000
; 'vT' Assigns selected value to variable %T%.
Gui, Add, Button, gStart1 x346 y27 w50 h20, Start
Gui, Add, Button, gStop1 x406 y27 w50 h20, Stop
; Now we added so called g_label to the buttons.
; In this case its "gStart1/gStart2" and similar.
; It will let us to have many buttons with same titles but assigned to different functions.
; Note, if you assigned some g-labels, then bottom in the code you must definie the function.
; Example: 'gStart1' have 'Start1:'. If there wont be function assigned, program will close.
; To find out what buttons do, read below, the functions assigned to them.
Gui, Add, Text, x16 y57 w70 h20 , Select Hotkey:
Gui, Add, DropDownList, x96 y57 w70 h180 v2H, F1|F2|A|B||C|D|E|F|
Gui, Add, Text, x176 y57 w90 h20 , Select Timing [ms]:
Gui, Add, ComboBox, x276 y57 w60 h180 v2T, 1000|2000||3000|4000|5000
Gui, Add, Button, gStart2 x346 y57 w50 h20, Start
Gui, Add, Button, gStop2 x406 y57 w50 h20, Stop
Gui, Show, x243 y144 h99 w477, Sample
;Following code will let our application hide to tray by double clicking the tray icon or when clicking the X 'close' button:
Menu, Tray, NoStandard
Menu, Tray, add, Hide|Show, Hide_Show_Win
Menu, Tray, add, Exit, Exit_Win
Menu, Tray, Default, Hide|Show
Main_Window := WinExist("A")
Return
;############# Minimize to Tray #############
Hide_Show_Win:
IfWinExist, ahk_id %Main_Window%
Gui, Hide
else
Gui, Show
Return
; While selecting EXIT from Tray Icon Menu, application will be closed.
Exit_Win:
ExitApp
Return
; While pressing X button, application will be minimized to Tray Icon:
GuiClose:
Gui, Submit
Menu, Tray, Icon
return
;############# Buttons & Functions #############
;Here is the part of gStart1:
Start1:
Gui, Submit, Nohide
; To save the values from ComboBox and DropDownList in to memory.
SetTitleMatchMode 3
; To specify that only exact name of application will be count.
WinGet, note, ID, Bez tytułu - Notatnik
; To assign Notepad's ID with our variable 'note'.
; PS: I'm Polish, so in my language: Bez tytułu - Notatnik = Untitled - Notepad
IfWinNotActive, ahk_id %note%
WinActivate, ahk_id %note%
; Activating application assigned in previous line.
loop
{
ControlSend,, {%H%}, ahk_id %note%
; Will send a value:key specified in form DropDownList (H) to Bez Tytułu - Notatnik %note%.
Sleep, %T%
; Will sleep (delay) previous function with amount of millisecions specified in ComboBox as %T%.
if (T < 0 )
; Gives an option to break the key sending function (connected to ButtonStop).
Break
}
Return
;Here is the part of gStop1:
Stop1:
T = -1
; When used, changes the value of %T% to negative (less than zero) what cause end of the loop.
Return
;Here is the part of gStart2:
Start2:
Gui, Submit, Nohide
SetTitleMatchMode 3
WinGet, note2, ID, Bez tytułu - Notatnik
IfWinNotActive, ahk_id %note2%
WinActivate, ahk_id %note2%
loop
{
ControlSend,, {%2H%}, ahk_id %note2%
Sleep, %2T%
if (2T < 0 )
Break
}
Return
;Here is the part of gStop2:
Stop2:
2T = -1
Return
2nd Code:
Code:
#SingleInstance, off
Gui, Font, s8, Verdana
Gui, Add, GroupBox, x6 y7 w460 h80 , Mouse
Gui, Add, Text, x16 y27 w120 h20 , Select Timing [ms]:
Gui, Add, ComboBox, x136 y27 w100 h180 vT, 1000|2000|3000|4000|5000||
Gui, Add, Button, x246 y27 w100 h20 , Start
Gui, Add, Button, x356 y27 w100 h20 , Stop
Gui, Add, Button, x356 y57 w100 h20 , Exit
Gui, Show, x194 y258 h102 w477, Sample
Return
ButtonStart:
Gui, Submit, Nohide
SetTitleMatchMode, 3
WinGet, note, ID, Bez tytułu - Notatnik
IfWinNotActive, ahk_id %note%
WinActivate, ahk_id %note%
loop
{
; Are 2 types of cursors, both known for AutoHotkey as 'unknown'.
; Lets say there is 1, standard arrow, while moving on specified target changes to 2.
; I have no idea how works the DLLCall or if Autohotkey can detect if
; from cursor changed from unknown1 to unknown2.
MouseMove, 512, 384
if (Cursor changed) then
Click, 512, 384
sleep %T%
MouseMove, 530, 400
if (Cursor changed) then
Click, 530, 400
if (T < 0)
Break
}
ButtonStop:
T = -1
Return
ButtonExit:
GuiClose:
ExitApp
Currently working on:
- Question 2: Problem with functions assigned to different buttons with same labels.
- Question 3: Can't find easy way to minimize to tray (remove from task bar)
- Question 4: Cannot find a solution to check if cursor changed after mouse move. Note, cursors there are not standard ones - are image based.
[Moved from General Chat forum. ~jaco0646]