Jump to content

Question with WinWaitActive


Ace08
 Share

Recommended Posts

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. :graduated:

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

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. :graduated:

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

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

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

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 :graduated:

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

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

Wow Thanks Tvern its working great! :(

i've used the 2nd solution since it best describes what i need

Thanks again for the help :graduated:

(So not adding a function to hotkey will unset it, nice :D)

Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...