Announcement

Collapse
No announcement yet.

Script for timed keystrokes in an active program

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script for timed keystrokes in an active program

    I am trying to write a script that will make certain keystrokes (F5 and F6) every 2 min or so in another application. This is purely for entertainment purposes so obviously not that important. It is for a game I am playing wherein I need to hit a few keys every 2 minutes (120 sec) over and over and over again. Tried on my own and failed miserably.

    I know it will have to call the active window first then have a delay for 120 sec but putting it all together is not working. Thanks in advance.

  • #2
    Re: Script for timed keystrokes in an active program

    Autohotkey is the tool for that.
    And then REMS will come along and script it
    "...if I turn out to be particularly clear, you've probably misunderstood what I've said” - Alan Greenspan

    Comment


    • #3
      Re: Script for timed keystrokes in an active program

      You can also use the SendKeys function of the Windows Script Host

      Code:
      ' www.winstructor.com/articles/vbscript/sendkeys.htm
      ' ----------------------------------------------------------
      
      ' To set focus you can use the window title of the application
      ' Or use the PID (unique process ID), when there is no unique window title. You can retrieve the PID when starting the application by script.
      
      ' Name (unique set of first characters of the *Application's window title*, check Taskmanager/Applications for the exact name)
      strNameApp = "petri.co.il forums by" 
      
      strtimer = 6e4*(2)  '<= sleep 2 minutes 
      strMaxLoops = 10  'times
      
      Do until i > strMaxLoops
         i = i + 1
         call SendKeysTo(strNameApp)
         wsh.sleep strtimer
      Loop
      
      
      Sub SendKeysTo(strNameApp)
      With WScript.CreateObject("WScript.Shell")
      
      '- It is always good to add 5 to 99 miliseconds delay between series of controling keys
      '- Additional delays between keystrokes will depend on how the application is performing on the computer
      '  (you may have to fine tune the delays).
      '- After each (long or very short) delay set focus again, then only sendkeys if focused.
      
       ' Maximize or Repair window [ (Alt+space) M ]
       '--------------------------------------------
         Wsh.sleep 99 : .AppActivate(strNameApp)
         If .AppActivate(strNameApp) = True then
           .SendKeys "(% )" ' Alt+space
           Wsh.sleep 5 : .AppActivate(strNameApp)
           If .AppActivate(strNameApp) = True _
             then .SendKeys "M"  'check your localized Windows version for the right letter (case preference!)
         End If
         
       ' Send keystrokes F4 and F5
       '--------------------------------------------
         Wsh.sleep 99 : .AppActivate(strNameApp)
         If .AppActivate(strNameApp) = True _
           Then .SendKeys "{F4}"
      
         Wsh.sleep 99 : .AppActivate(strNameApp)
         If .AppActivate(strNameApp) = True _
           Then .SendKeys "{F5}"
      
       ' Minimize window [ (Alt+space) n ]
       '--------------------------------------------
         Wsh.sleep 99 : .AppActivate(strNameApp)
         If .AppActivate(strNameApp) = True then
           .SendKeys "(% )" ' Alt+space
           Wsh.sleep 5 : .AppActivate(strNameApp)
           If .AppActivate(strNameApp) = True _
             then .SendKeys "n"  'check your localized Windows version for the right letter (case preference!)
         End If
      
       ' release focus
       '---------------------------
         Wsh.sleep 99 : .SendKeys("^")
      End With
      End Sub
      (You might have to deactivate the screensaver first)


      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Script for timed keystrokes in an active program

        Thanks guys. Lior_S, I ended up using an app much like the one you suggested but made specifically for games called ACTool. It worked like a charm and have been using it successfully for my gaming needs.

        REMS, genius as always. Although the ACTool is fitting the bill, I am going to use your suggestions anyway as it always furthers my scripting know-how. You guys rock and so does this site.

        Comment

        Working...
        X