Ace08 Posted November 22, 2010 Share Posted November 22, 2010 Hi guys, I have this code below Global $Paused,$pos1,$pos2 If WinWaitActive("Untitled") Then HotKeySet("p", "KeyPrint") ;p HotKeySet("c", "KeyCopy") ;c Endif While 1 Sleep(100) WEnd Func KeyPrint() Send("Printing Document {F5}") Send("^p") ;Send("{Enter}") EndFunc Func KeyCopy() Send("^a") Sleep(100) Send("^c") EndFunc the above code works, if Untitled isn't active it does nothing, if Untitled is active when i press p it would add "Printing Document" and the date before it prints it. but the problem is after i close Untitled or say open a new document and press "p" or "c" the script still do the functions for printing and copying. question is isn't WinWaitActive waits for the window to be active then if it isn't do nothing? Also i have tried WinActive but got the same result. Work smarter not harder.My First Posted Script: DataBase Link to comment Share on other sites More sharing options...
JohnOne Posted November 22, 2010 Share Posted November 22, 2010 My guess is that you have other windows named "untitled", your best bet would be to use a unique identifier of the window you want to work with, such as its handle. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Ace08 Posted November 22, 2010 Author Share Posted November 22, 2010 Thanks JohnOne for the fast reply but unfortunately i have none, i've used notepad++ writing this script and the best way to test this would be opening up a notepad which would be "untitled" but after i close the notepad and press "p" nor "c" the script does the functions, by the way the script name was printorcopy.au3 so im pretty sure i have no untitled application. Work smarter not harder.My First Posted Script: DataBase Link to comment Share on other sites More sharing options...
Tvern Posted November 22, 2010 Share Posted November 22, 2010 The problem is that the script only checks to see if the window is active once. Once a match has been found, the hotkeys are set and stay set untill the script ends. Instead you want it to check if the window is active each time you press a hotkey, like this: Global $sWinTitle = "Untitled" HotKeySet("p", "KeyPrint") ;p HotKeySet("c", "KeyCopy") ;c While 1 Sleep(100) WEnd Func KeyPrint() If Not WinActive($sWinTitle) Then Return Send("Printing Document {F5}") Send("^p") ;Send("{Enter}") EndFunc Func KeyCopy() If Not WinActive($sWinTitle) Then Return Send("^a") Sleep(100) Send("^c") EndFunc Link to comment Share on other sites More sharing options...
SadBunny Posted November 22, 2010 Share Posted November 22, 2010 What your script does now is: 1) Wait until WinWaitActive finds the window 2) IF (WHEN) it does, then it sets the hotkeys 3) After that, the hotkeys always work from the while loop I don't know if you want to only set the hotkeys after the window has been found (which is thinkable - if so then that is correct, if not then don't put the sethotkeys in that IF. But I guess you want the hotkeys to wait for a window AFTER you press the hotkey. So you should put the IF that waits for the window in the function that is executed when you press the hotkey. Something like: Global $Paused, $pos1, $pos2 HotKeySet("p", "KeyPrint") ;p HotKeySet("c", "KeyCopy") ;c While 1 Sleep(100) WEnd Func KeyPrint() If WinWaitActive("Untitled") Then Send("Printing Document {F5}") Send("^p") ;Send("{Enter}") EndIf EndFunc ;==>KeyPrint Func KeyCopy() If WinWaitActive("Untitled") Then Send("^a") Sleep(100) Send("^c") EndIf EndFunc ;==>KeyCopy Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
Ace08 Posted November 22, 2010 Author Share Posted November 22, 2010 Thanks SadBunny and Tvern i've tried both your codes, now the problem is if there is no untitled active say i want to type powerpoint the script does nothing when i press p so the word powerpoint would look oweroint with no p's Work smarter not harder.My First Posted Script: DataBase Link to comment Share on other sites More sharing options...
Tvern Posted November 22, 2010 Share Posted November 22, 2010 Yeah hotkeyset captures the key pressed and stops it from being send to the active window. It's a very common thing people encounter and it can be solved in a few ways depending on your preference: 1st solution: make autoit send the key you pressed Global $sWinTitle = "Untitled" HotKeySet("p", "KeyPrint") ;p HotKeySet("c", "KeyCopy") ;c While 1 Sleep(100) WEnd Func KeyPrint() If WinActive($sWinTitle) Then Send("Printing Document {F5}") Send("^p") Else HotKeySet("p") ;unset hotkey to avoid a trigger by Send() Send("p") ;send p HotKeySet("p", "KeyPrint") ;re-set hotkey EndIf ;Send("{Enter}") EndFunc Func KeyCopy() If WinActive($sWinTitle) Then Send("^a") Sleep(100) Send("^c") Else HotKeySet("c") ;unset hotkey to avoid a trigger by Send() Send("c") ;send c HotKeySet("c", "KeyCopy") ;re-set hotkey EndIf EndFunc 2nd solution: only set the hotkey when the application is active. Global $sWinTitle = "Untitled" While 1 WinWaitActive($sWinTitle) HotKeySet("p", "KeyPrint") ;set hotkeys while the window is active HotKeySet("c", "KeyCopy") ;c WinWaitNotActive($sWinTitle) HotKeySet("p") ;unset hotkeys while the window is inactive HotKeySet("c") WEnd Func KeyPrint() Send("Printing Document {F5}") Send("^p") ;Send("{Enter}") EndFunc Func KeyCopy() Send("^a") Sleep(100) Send("^c") EndFunc 3rd solution: Instead of HotKeySet(), use _IsPressed(), which doesn't stop the key from being send. #include <misc.au3> While 1 If _IsPressed("50") Then KeyPrint() ;check for p If _IsPressed("43") Then KeyCopy() ;check for c Sleep(10) WEnd Func KeyPrint() If WinActive($sWinTitle) Then Send("{BS}") ;delete the p you just typed. Send("Printing Document {F5}") Send("^p") EndIf While _IsPressed("50") ;wait for p to be released Sleep(10) WEnd ;Send("{Enter}") EndFunc Func KeyCopy() If WinActive($sWinTitle) Then Send("{BS}") ;delete the c you just typed. Send("^a") Sleep(100) Send("^c") EndIf While _IsPressed("43") ;wait for c to be released Sleep(10) WEnd EndFunc Looking back at that I think I prefer the 2nd solution, which is also the most like what you had initially. Link to comment Share on other sites More sharing options...
Ace08 Posted November 23, 2010 Author Share Posted November 23, 2010 (edited) Wow Thanks Tvern its working great! i've used the 2nd solution since it best describes what i need Thanks again for the help (So not adding a function to hotkey will unset it, nice ) Edited November 23, 2010 by Ace08 Work smarter not harder.My First Posted Script: DataBase Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now