0
votes

I have a GUI menu in AutoHotKey that I am having trouble with. The GUI has three buttons named "Red", "Blue" and "Green".

When the user clicks on say, "Red", an action happens. If the user clicks on say, "Blue", a different action happens.

The loop is started with the F4 key, but a different thing should loop depending on which button was pressed: Red, Blue or Green.

This is the script I have so far:

Gui, Add, Tab, x366 y377 w-100 h-400 , Tab1
Gui, Add, Picture, x6 y7 w320 h90 , C:\IMAGEFILEHERE
Gui, Add, Text, x46 y147 w140 h40 , Spawning Team
Gui, Add, Button, x206 y117 w120 h20 , Red
Gui, Add, Button, x206 y147 w120 h20 , Blue
Gui, Add, Button, x206 y177 w120 h20 , Green
Gui, Show, x436 y230 h208 w336, SCRIPTNAME

Loop 
{ 
  If run = 
  {
  sleep,250 
  continue
}
  Else 
  { 
if (Team = "Red") ; If Red is selected, run this part
{
    ACTION1HERE
}
if (Team = "Blue") ; If Blue is selected, run this part
{
    ACTION2HERE
}
if (Team = "Green") ; If Green is selected, run this part
{
    ACTION3HERE
}
  } 
} 
return 

F4:: 
If run =  
  run = 1 
Else 
  run = 
return


ButtonRed:
Team = Red.
MsgBox The value in the variable named Team is %Team%.
Return

ButtonBlue:
Team = Blue.
MsgBox The value in the variable named Team is %Team%.
Return

ButtonGreen:
Team = Green.
MsgBox The value in the variable named Team is %Team%.
Return

The problem is that the button being pressed is not being detected by the if statement.

Any help is much appreciated! ^_^ I am quite new to AHK.

2

2 Answers

3
votes

Add a gRed , gBlue and gGreen label to each add button, then create 3 labels (go to addresses)

Gui, Add, Tab, x366 y377 w-100 h-400 , Tab1
Gui, Add, Picture, x6 y7 w320 h90 , C:\IMAGEFILEHERE
Gui, Add, Text, x46 y147 w140 h40 , Spawning Team
Gui, Add, Button, x206 y117 w120 h20 gRed, Red
Gui, Add, Button, x206 y147 w120 h20 gBlue, Blue
Gui, Add, Button, x206 y177 w120 h20 gGreen, Green
Gui, Show, x436 y230 h208 w336, SCRIPTNAME

Red:
MsgBox, ACTION1HERE
Return

Blue:
MsgBox, ACTION2HERE
Return

Green:
MsgBox, ACTION3HERE
Return
0
votes

The loop probably prevents the lower script to be happen. Try Settimer instead

So instead of

Loop 
{ 
  If run = 
  {
  sleep,250 
  continue
}
  Else 
  { 
if (Team = "Red") ; If Red is selected, run this part
{
    ACTION1HERE
}
if (Team = "Blue") ; If Blue is selected, run this part
{
    ACTION2HERE
}
if (Team = "Green") ; If Green is selected, run this part
{
    ACTION3HERE
}
  } 
} 
return 

F4:: 
If run =  
  run = 1 
Else 
  run = 
return

try

F4::
If run =  
{   
  run = 1
  Settimer, actionloop, 250
}
Else 
{
  run = 
  Settimer, actionloop, off
}
return

actionloop:
  if (Team = "Red") ; If Red is selected, run this part
  {
      ACTION1HERE
  }
  if (Team = "Blue") ; If Blue is selected, run this part
  {
      ACTION2HERE
  }
  if (Team = "Green") ; If Green is selected, run this part
  {
      ACTION3HERE
  }
return