Sign in to follow this  
Followers 0
Meerecat

What's wrong with my If statements?

8 posts in this topic

Hello

I am relatively new to autoit. I am trying to achieve a message box appearing when you press a hotkey, depending on the running program. Here is my code:

HotKeySet("^+q", "quit")  ;Quit 
HotKeySet("^+{F1}", "main") 
Opt("WinTitleMatchMode", 2)   ;Allow any text in window title


While 1 ;Loop the program
WEnd

Func quit() ;Quit the program
    Exit
EndFunc 


Func main()
    If WinWaitActive("[CLASS:Notepad]", "") Then
    msgbox (0, "Notepad", "You are running Notepad")
    EndIf
    If WinWaitActive("[CLASS:IEFrame]", "") Then
    msgbox (0, "IE", "You are running IE")
    EndIf
    If WinWaitActive("[CLASS:MozillaWindowClass]", "") Then
    msgbox (0, "Firefox", "You are running Firefox")
    EndIf
EndFunc

I have a couple of problems with this:

1. If I start up Notepad and press the hotkey, the message box appears. If I then start Internet Explorer the message box appears automatically, rather than waiting for the hotkey. This happens with or without the If statements. How can I correct this?

2. It does not work at all in Firefox, even though I am using the class provided by AutoIt Window Info.

3. Sometimes it takes several hotkey presses to work at all, even in Notepad. Is there a more reliable way of achieving this?

Many Thanks

Lee


Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Don't use WinWaitActive() as that will stop the script from continuing until that Window becomes active, but use WinExists().

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

You should be using ElseIfs in there or better yet get rid of the WinWaitActive and just use WinActive. Also It's not a great idea to set a hotkey and then run functions where one of the other apps may need that HK combo. Better to unset it and then reset it.

Func main()
    HotKeySet("^+{F1}")
    Local $sVal = ""
    If WinActive("[CLASS:Notepad]", "") Then
        $sVal = "Notepad"
    ElseIf WinActive("[CLASS:IEFrame]", "") Then
        $sVal = "IE"
    ElseIf WinActive("[CLASS:MozillaWindowClass]", "") Then
        $sVal = "FireFox"
    EndIf
    If $sVal <> "" Then msgbox (0, $sVal, "You are running " & $sVal)
    HotKeySet("^+{F1}", "main")
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

You should be using ElseIfs in there or better yet get rid of the WinWaitActive and just use WinActive. Also It's not a great idea to set a hotkey and then run functions where one of the other apps may need that HK combo. Better to unset it and then reset it.

Func main()
    HotKeySet("^+{F1}")
    Local $sVal = ""
    If WinActive("[CLASS:Notepad]", "") Then
        $sVal = "Notepad"
    ElseIf WinActive("[CLASS:IEFrame]", "") Then
        $sVal = "IE"
    ElseIf WinActive("[CLASS:MozillaWindowClass]", "") Then
        $sVal = "FireFox"
    EndIf
    If $sVal <> "" Then msgbox (0, $sVal, "You are running " & $sVal)
    HotKeySet("^+{F1}", "main")
EndFunc

Thanks for this, but I can't make it work. Pressing the hotkey with Notepad active doesn't display the message box. Also I need that hotkey to be available all the time. The program will run constantly in the system tray, so do I still need to release the hotkey?

Anyway here is the code I am using, I think it is the same as yours:

While 1 ;Loop the program
WEnd

main()

Func main()
    HotKeySet("^+{F1}")
    Local $sVal = ""
    If WinActive("[CLASS:Notepad]", "") Then
        $sVal = "Notepad"
    ElseIf WinActive("[CLASS:IEFrame]", "") Then
        $sVal = "IE"
    ElseIf WinActive("[CLASS:MozillaWindowClass]", "") Then
        $sVal = "FireFox"
    EndIf
    If $sVal <> "" Then msgbox (0, $sVal, "You are running " & $sVal)
    HotKeySet("^+{F1}", "main")
EndFunc

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites

Don't use WinWaitActive() as that will stop the script from continuing until that Window becomes active, but use WinExists().

I tried WinExists() but this makes the message box appear regardless of whether the window is active.

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites

This is working fine for me

Opt ('TrayIconDebug',1)

HotKeySet("^+q", "quit")  ;Quit
HotKeySet("^+{F1}", "main")
Opt("WinTitleMatchMode", 2)   ;Allow any text in window title


While 1 ;Loop the program
WEnd

Func quit() ;Quit the program
    Exit
EndFunc

Func main()
    HotKeySet("^+{F1}")
    Local $sVal = ""
    If WinActive("[CLASS:Notepad]", "") Then
        $sVal = "Notepad"
    ElseIf WinActive("[CLASS:IEFrame]", "") Then
        $sVal = "IE"
    ElseIf WinActive("[CLASS:MozillaWindowClass]", "") Then
        $sVal = "FireFox"
    EndIf
    If $sVal <> "" Then msgbox (0, $sVal, "You are running " & $sVal)
    HotKeySet("^+{F1}", "main")
EndFunc

Not tested with FF because I won't install it.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

This is working fine for me

Opt ('TrayIconDebug',1)

HotKeySet("^+q", "quit")  ;Quit
HotKeySet("^+{F1}", "main")
Opt("WinTitleMatchMode", 2)   ;Allow any text in window title


While 1 ;Loop the program
WEnd

Func quit() ;Quit the program
    Exit
EndFunc

Func main()
    HotKeySet("^+{F1}")
    Local $sVal = ""
    If WinActive("[CLASS:Notepad]", "") Then
        $sVal = "Notepad"
    ElseIf WinActive("[CLASS:IEFrame]", "") Then
        $sVal = "IE"
    ElseIf WinActive("[CLASS:MozillaWindowClass]", "") Then
        $sVal = "FireFox"
    EndIf
    If $sVal <> "" Then msgbox (0, $sVal, "You are running " & $sVal)
    HotKeySet("^+{F1}", "main")
EndFunc

Not tested with FF because I won't install it.

Works a charm, thank you very much :huh2:

By the way, your code works in Firefox where as mine didn't. So thanks again ;)

Edited by Meerecat

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites

No problem. Glad it worked.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0