Meerecat 0 Posted June 5, 2011 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
Jos 2,209 Posted June 5, 2011 (edited) Don't use WinWaitActive() as that will stop the script from continuing until that Window becomes active, but use WinExists(). Edited June 5, 2011 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Share this post Link to post Share on other sites
GEOSoft 67 Posted June 5, 2011 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 GeorgeQuestion 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
Meerecat 0 Posted June 5, 2011 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
Meerecat 0 Posted June 5, 2011 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
GEOSoft 67 Posted June 5, 2011 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. GeorgeQuestion 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
Meerecat 0 Posted June 5, 2011 (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 By the way, your code works in Firefox where as mine didn't. So thanks again Edited June 5, 2011 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
GEOSoft 67 Posted June 5, 2011 No problem. Glad it worked. GeorgeQuestion 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