Script for timed keystrokes in an active program

Home Forums Scripting General Scripting Script for timed keystrokes in an active program

This topic contains 3 replies, has 3 voices, and was last updated by CaTBuS CaTBuS 11 years, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • CaTBuS
    CaTBuS
    Participant
    #130901

    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.

    Avatar
    Lior_S
    Member
    #282654

    Re: Script for timed keystrokes in an active program

    Autohotkey is the tool for that.
    And then REMS will come along and script it :grin:

    Rems
    Rems
    Moderator
    #226761

    Re: Script for timed keystrokes in an active program

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

    ‘ [URL=http://www.winstructor.com/articles/vbscript/sendkeys.htm]www.winstructor.com/articles/vbscript/sendkeys.htm[/URL]
    ‘ ———————————————————-

    ‘ To set focus you can use the [B]window title[/B] of the application
    ‘ Or use the [B]PID[/B] (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 = “[COLOR=”RoyalBlue”]petri.co.il forums by[/COLOR]”

    strtimer = [COLOR=”royalblue”]6e4*(2)[/COLOR] ‘ 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 “[COLOR=”royalblue”]M[/COLOR]” ‘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 “[COLOR=”royalblue”]{F4}[/COLOR]”

    Wsh.sleep 99 : .AppActivate(strNameApp)
    If .AppActivate(strNameApp) = True _
    Then .SendKeys “[COLOR=”royalblue”]{F5}[/COLOR]”

    ‘ 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 “[COLOR=”RoyalBlue”]n[/COLOR]” ‘check your localized Windows version for the right letter (case preference!)
    End If

    ‘ release focus
    ‘—————————
    Wsh.sleep 99 : .SendKeys(“^”)
    End With
    End Sub[/CODE]
    (You might have to deactivate the screensaver first)

    Rems[CODE]
    http://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[/CODE]
    (You might have to deactivate the screensaver first)

    Rems

    CaTBuS
    CaTBuS
    Participant
    #309103

    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.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Register for this Petri Webinar!

Want to Make Your Backup Storage Unlimited & Ready for the Cloud? – Free Thurrott Premium Account with Webinar Registration!

Tuesday, August 27, 2019 @ 1:00 pm EDT

A Scale-Out Backup storage infrastructure is a must-have technology for your backups. In this webinar, join expert Rick Vanover for a look on what real-world problems are solved by the Scale-Out Backup Repository.

Register Now

Sponsored By