Jump to content

Send keystrokes to idle application only once


nyedfam
 Share

Recommended Posts

I am trying to send  the keystrokes below to application whenever it stays idle for 5 minutes.

I able to send the keys but timer doesn't reset when application is active. It loops when application is active. The application becomes active with mouse movement, keystrokes and window refresh.  I want the script to script to sleep when the application and only runs once when it becomes idle. See code below. Thanks

 

Global $TIMER = TimerInit()
 local  $var = WinGetTitle("Untitled - Notepad")

While 1

    ;If this application is active then reset the timer:
    If WinActive($var) Then
        $TIMER = TimerInit()
    EndIf

    ;if the timer is more than 5 minutes then:
    If TimerDiff($TIMER) >= 5 * 60 * 1000 Then
 
        Send("{CTRLDOWN}{F1}")

    EndIf

    ;Sleep for 1 seconds before looping again:
    Sleep(10000)
WEnd

Link to comment
Share on other sites

You should reinitialize the timer inside the If statement right after you use the Send function.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I reinitialized the timer, but script still execute when window is active. See script below.

 

 

Global $TIMER = TimerInit()

 local  $var = WinGetTitle("Untitled - Notepad")

While 1

    ;If this application is active then reset the timer:
    If WinActive($var) Then
        $TIMER = TimerInit()
    EndIf

    ;if the timer is more than 5 minutes then:
    If TimerDiff($TIMER) >= 5 * 60 * 1000 Then
                 Send("{CTRLDOWN}{F1}")

           ; Set $timer value to 0

$TIMER = TimerInit()

    EndIf
    Sleep(10000)
WEnd

Link to comment
Share on other sites

Your script doesn't make a lot of sense, first, you're getting the Title of a window that you already have the title of.

Local  $var = WinGetTitle("Untitled - Notepad")

Why are you doing that? What purpose do you think it's serving?

What might be happening is, if you don't have "notepad" open when you start this script, then $var will be an empty string, and your WinActive will never work because you used that unnecessary line above to get the title of the window.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I know what you're doing, but how you're doing it is all wrong. You get the title of the window before the While loop, using the title you already have, and never check to see if there's anything IN the return from WinGetTitle. If the window isn't open when you run the script, your WinActive line will never find it because the variable is empty. Get rid of the WinGetTitle line, and just set $var to the text of the window title you want to find. Then your script should work as you want it to.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

What do you mean by the script goes to sleep and never wakes? What are you expecting it to do that it's not doing?

As to the CPU usage, with a 10 second sleep every time you loop, I doubt the CPU is being used all that much.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

When there keyboard and mouse activities within notepad, the script never execute. Which is how I wanted. The only problem is , I  am expecting the script to execute every 10 minutes of inactivity, but it doesn't. Any work around this? I appreciate your help.

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