Control send in a Script doesn't work well - autohotkey

I am trying to make a script by myself in a game and I have a problem with the script. My problem is in the lines of code like this:
ControlSend, ahk_parent, {z down}, ahk_exe javaw.exe
sleep 4500
ControlSend, ahk_parent, {z up}, ahk_exe javaw.exe
The problem is that the action is not sent while the game is in the background. But instead in these instructions it did work:
ControlSend, ahk_parent, 3, ahk_exe javaw.exe
sleep 3800
I don't understand the problem with commanding a key down versus sending a key.
I leave here a sample of the code so that you can inspect it in case you find any errors.
ControlSend, ahk_parent, 3, ahk_exe javaw.exe
sleep 3800
ControlSend, ahk_parent, {z down}, ahk_exe javaw.exe
sleep 4500
ControlSend, ahk_parent, {z up}, ahk_exe javaw.exe
sleep, 1000
ControlSend, ahk_parent, {down down}, ahk_exe javaw.exe
sleep, 1000
ControlSend, ahk_parent, {down up}, ahk_exe javaw.exe
sleep, 1250
ControlSend, ahk_parent, 2 , ahk_exe javaw.exe
sleep, 0100
ControlSend, ahk_parent, {right down}, ahk_exe javaw.exe
sleep, 2000
ControlSend, ahk_parent, {right up}, ahk_exe javaw.exe
ControlSend, ahk_parent, {down down}, ahk_exe javaw.exe
sleep, 0325
ControlSend, ahk_parent, {down up}, ahk_exe javaw.exe
ControlSend, ahk_parent, {right down}, ahk_exe javaw.exe
sleep, 0325
ControlSend, ahk_parent, {right up}, ahk_exe javaw.exe
sleep, 0100
ControlSend, ahk_parent, {z up}, ahk_exe javaw.exe
sleep, 0100
ControlSend, ahk_parent, {z down}, ahk_exe javaw.exe
sleep, 2000
Loop, 6
{
ControlSend, ahk_parent, 1 , ahk_exe javaw.exe
sleep, 14000
ControlSend, ahk_parent, {z down} , ahk_exe javaw.exe
ControlSend, ahk_parent, {z up} , ahk_exe javaw.exe
sleep, 0100
ControlSend, ahk_parent, {z down} , ahk_exe javaw.exe
ControlSend, ahk_parent, {z up} , ahk_exe javaw.exe
sleep, 0100
ControlSend, ahk_parent, {z down} , ahk_exe javaw.exe
ControlSend, ahk_parent, {z up} , ahk_exe javaw.exe
sleep, 0100
ControlSend, ahk_parent, {z down} , ahk_exe javaw.exe
ControlSend, ahk_parent, {z up} , ahk_exe javaw.exe
sleep, 12900
}
return
I have to say that all the script with Send works correctly, only now I wanted to improve it so that it works in the background.
Thanks.

It will never work in the background using autohotkey or the like (accessing the keyboard or mouse) because those UIDs only ever interact with the active window (the exception being software for instance to scroll an inactive window with the mouse wheel - but even then, you have to be over the specific program and can't have the mouse doing something else).
You need a COM interface or api to access to the underlying control element itself. What does Window Spy tell you about the control and contents?
Sorry but if it were that easy the games would find better ways to block AHK and other macro programs from the outset, but good luck in finding a solution (can you run a parallel instance that you can keep active in a virtual machine?)

Related

autoHotkey close browser if browser is already open

If browser is not already open. make the browser open.
If browser is already open. make the browser close.
I've found a script but this does not close the browser
!f::
firefox = C:\Program Files\Mozilla Firefox\Firefox.exe
IfWinExist, firefox ; Title of the prog
{
WinActivate
WinClose
}
else
{
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "github.com"
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "stackoverflow.com"
}
How can I close the program already exists?
Try with this:
!f::
firefox = C:\Program Files\Mozilla Firefox\Firefox.exe
IfWinExist, "ahk_exe firefox.exe" ; Executable name
{
WinActivate
WinClose
}
else
{
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "github.com"
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "stackoverflow.com"
}

Is there a way to move arrow key to left on Windows Popup?

I am writing a script which will allow PowerShell to run in admin mode but when I run Set-ExecutionPolicy Bypass, Windows displays a popup and I use my left arrow key to move to the yes button and press enter.
The problem I am facing is that it does not move to the left or press enter when I run the script as follows.
RunWait PowerShell.exe -ExecutionPolicy Bypass -NoExit -Command "C:\Users\%A_UserName%\Desktop\Script.ps1";
Send {Left}
Sleep, 5000
Send {Enter}
I am expecting it to actually move to the OK button on the left and press enter but nothing actually happens.
If you run PowerShell in admin mode your script needs also to be run
in this mode.
Unlike Run, RunWait will wait until the program (in
this case PowerShell.exe) finishes before continuing (= sending
commands to the popup window). To bypass this problem use SetTimer.

Wscript .vbs file not running in task scheduler in windows 10

I have a wscript to run a .vbs file on my desktop. And I want it to run every day at a certain time. I set up a task in the Windows 10 to run the script, but nothing happens at the designated time. Under the "action" tab in the scheduler I have wscript.exe in the Program/Script: input line, and the file path in the Add Arguments line. When I simply double click on the .vbs file on my desktop, it works just fine, but for some reason the task scheduler won't start it, even when I try clicking "Run" in the scheduler to test. I've been reading on some forums that maybe it needs to be a cscript.exe, but I've tried that it doesn't work. My .vbs file does use wscirpt, so maybe I need to change the script to cscript at the same time and rerun it. I have tried that, but it doesn't seem to work. Below the script in my .vbs file.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
WScript.Sleep 3000
WshShell.AppActivate "Cisco AnyConnect Secure Mobility Client"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "XXXXX"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\carls\Desktop\REIT Model\Model Forward\Stock Model REITs.xlsm")
objExcel.Application.Visible = True
objExcel.Application.Run "PortfolioEvaluator"
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "outlook"
WScript.Sleep 30000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
WScript.Sleep 3000
WshShell.AppActivate "Cisco AnyConnect Secure Mobility Client"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"

How do you close a program as soon as you open it using a batch file?

I am trying to close firefox as soon as I open it, and then reopen it again in a batch file.
This is what i have below:
#ECHO off
"C:\Program Files\Mozilla Firefox\firefox.exe"
sleep 10
taskkill /F/IM "C:\Program Files\Mozilla Firefox\firefox.exe"
sleep 10
"C:\Program Files\Mozilla Firefox\firefox.exe"
However it doesnt work, it just opens firefox and never closes it.
The system I am trying to get this to work on is Windows 7 Home Premium 64bit. I don't know if that makes much of a difference.
Any help on this will be greatly appreciated. Thanks.
Taskkill is used to kill the process.
Try
taskkill /F/IM firefox.exe
Here is the documentation from MSDN
http://technet.microsoft.com/en-us/library/bb491009.aspx

Schedule a .vbs file to run in Windows

I have a VBScript script that starts a cmd prompt, telnets into a device and TFTP's the configuration to a server. It works when I am logged in and run it manually. I would like to automate it with Windows Task Scheduler.
Any assistance would be appreciated, here is the VBScript script:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd"
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 300
WshShell.SendKeys "telnet 10.20.70.254{ENTER}"
WScript.Sleep 300
WshShell.SendKeys "netscreen"
WScript.Sleep 300
WshShell.SendKeys "{ENTER}"
WScript.Sleep 300
WshShell.SendKeys "netscreen"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 300
WScript.Sleep 300
WshShell.SendKeys "save conf to tftp 10.10.40.139 test.cfg{ENTER}"
WScript.Sleep 200
WshShell.SendKeys "exit{ENTER}" 'close telnet session'
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd"
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 300
WshShell.SendKeys "telnet 10.20.70.254{ENTER}"
WScript.Sleep 300
WshShell.SendKeys "netscreen"
WScript.Sleep 300
WshShell.SendKeys "{ENTER}"
WScript.Sleep 300
WshShell.SendKeys "netscreen"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 300
WScript.Sleep 300
WshShell.SendKeys "save conf to tftp 10.10.40.139 palsg140.cfg{ENTER}" 'repeat as needed
WScript.Sleep 200
WshShell.SendKeys "exit{ENTER}" 'close telnet session'
WshShell.SendKeys "{ENTER}" 'get command prompt back
WScript.Sleep 200
WshShell.SendKeys "exit{ENTER}" 'close cmd.exe
WshShell.SendKeys "{ENTER}" 'get command prompt back
WScript.Sleep 200
WshShell.SendKeys "exit{ENTER}" 'close cmd.exe
Add a scheduled task that runs the script with your credentials. Remind yourself that you need to update the credentials on the task every time you change your password. It be a good idea to have the script "phone home" via email or something every time it is run so that you can tell if it is being executed.
It might also be a good idea to set up a separate service id for these sorts of activities. You may not need to change the password on the service id as frequently.
You can add a scheduled task and enter no credentials or password for it. This will cause it to run under LOCAL SYSTEM (which normally is the context the Task Scheduler service uses).
Be aware that this is a backdoor vulnerability scenario: Anyone allowed to edit your script file could misuse it to do undesirable things on the machine that runs the task. Put proper permission on the script file to prevent that. On the other hand - a task running as LOCAL SYSTEM cannot wreck havoc over the network.
I propose you condense your script file a little:
Set WshShell = WScript.CreateObject("WScript.Shell")
Run "cmd.exe"
SendKeys "telnet 10.20.70.254{ENTER}"
SendKeys "netscreen"
SendKeys "{ENTER}"
SendKeys "netscreen"
SendKeys "{ENTER}"
SendKeys "save conf to tftp 10.10.40.139 test.cfg{ENTER}"
SendKeys "exit{ENTER}" 'close telnet session'
Run "cmd.exe"
SendKeys "telnet 10.20.70.254{ENTER}"
SendKeys "netscreen"
SendKeys "{ENTER}"
SendKeys "netscreen"
SendKeys "{ENTER}"
SendKeys "save conf to tftp 10.10.40.139 palsg140.cfg{ENTER}" 'repeat as needed
SendKeys "exit{ENTER}" 'close telnet session'
SendKeys "{ENTER}" 'get command prompt back
SendKeys "exit{ENTER}" 'close cmd.exe
SendKeys "{ENTER}" 'get command prompt back
SendKeys "exit{ENTER}" 'close cmd.exe
Sub SendKeys(s)
WshShell.SendKeys s
WScript.Sleep 300
End Sub
Sub Run(command)
WshShell.Run command
WScript.Sleep 100
WshShell.AppActivate command
WScript.Sleep 300
End Sub
I'm pretty sure SendKeys will not work if the desktop is locked or no user is logged in.
I'm pretty SendKeys will not work if you aren't logged in.
It's unreliable in my experience anyway.
You might be better off using a DOS batch file.
getftpconf.bat:
telnet 10.10.40.139
netscreen
netscreen
save conf to tftp 10.10.40.139 palsg140.cf
exit
Something like that.
If there is output in the command prompt that you need to record, you can put a " >> output.txt" at the end of the command line shortcut.
You could then call another batch file which sends that output.txt via ftp to where ever you need.
You can easily setup this batch file to run as a scheduled task in windows.
just make a batch file that contains this:
cscript.exe myscript.vbs
save it as something like myscript.bat.
Use schedule tasks to schedule the .bat file. After you create the scheduled task, you may have to check it's properties to make sure it's has appropriate user rights.
There are some options you can use with cscript so it doesn't show the logo, etc.
Batch files don't work in Windows with Telnet (works fine in UNIX -- again, way to go Microsoft). As already mentioned here, sendkeys does not work in vba when not logged on.
Sorry I don't have the "this does work" solution for you....I'm stuck on the same problem