AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

If / Else Question

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
swankinrosco



Joined: 18 Jun 2008
Posts: 6

PostPosted: Wed Jun 18, 2008 10:11 pm    Post subject: If / Else Question Reply with quote

Has anyone accomplished a script that has a keystroke command with an embedded if:else to determine output?

EG:


a::
if (variable = 1) {
Send Hello
variable = 2
return
}
if (variable = 2) {
Send Goodbye
variable = 3
return
}
return

I'm trying to accomplish something like this but having little luck

[ Moderator!: Moved from Scripts and Functions ]
[Title edited. Please write descriptive titles for your topics. ~jaco0646]
Back to top
View user's profile Send private message
Elesar



Joined: 28 Jun 2007
Posts: 98

PostPosted: Wed Jun 18, 2008 11:48 pm    Post subject: Reply with quote

First, wrong section. There is a Help section in the forum for a reason.

Second, Please use the code tags when posting code. This will make the code much easier to read.

Third, you may be looking for something like this:
Code:
#SingleInstance Force

Test := 0

!a::
  If Test = 0
    {
      MsgBox, Hello
      Test := 1
    }
  Else
    {
      If Test = 1
        {
          MsgBox, Goodbye
          Test := 0
        }
    }
  Return


That could probably be done much more efficiently by someone with more time / experience that I have, but that works.
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Jun 19, 2008 12:52 am    Post subject: Reply with quote

one-uping elesar
Code:
a::
  Var:=!Var   ; Flips var from 0 to 1 and viceversa
  If Var      ; Var=1
    Send Hi
  Else        ; Var=0
    Send Bye
Return

my two cents on what the guy wanted
Code:
a::
  ; Spells out hello when 'a' is pressed 5 times
  If Var=0
    Send h
  Else If Var=1
    Send e
  Else If (Var=3) || (Var=4)
    Send l
  Else If Var=5
    Send o
  Var++ ; Adds 1 to var
Return

next time post in the right section please
Back to top
Wouther



Joined: 01 May 2007
Posts: 79
Location: The Netherlands

PostPosted: Thu Jun 19, 2008 10:07 am    Post subject: Reply with quote

It can be even shorter, using the ternary operator! Cool
Code:
Test := 0

!a::
Send, % (Test = 0) ? "Hello" : "Goodbye"
Test := !Test
Return

_________________
Printing css/html-formatted text
Back to top
View user's profile Send private message
Elesar



Joined: 28 Jun 2007
Posts: 98

PostPosted: Thu Jun 19, 2008 1:39 pm    Post subject: Reply with quote

Thats the great thing about these forums, you'll get several possible answers to your question (If is it feasible).

I have learned something new too! Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group