简体   繁体   中英

What's the difference between Run command and commands in cmd?

I have the following script:

^!c::
Run stop
Return

Stop is configured to run a program via environment variables.

So if I open up cmd and type “stop” and hit enter the program opens as intended, even if I push winkey + R it does the same thing. However if I use the script with ctrl + alt + c . I do not get the same result.

Why is the script doing something different?

How can I change my script to behave the same way as if it was typed into cmd or winkey + R ?

Simple :

run, %comspec% /c stop

Or if this doesn't work you could just start a cmd window and send it directly

run, %comspec% /k
WinWait, %comspec%
WinActivate
Send stop{Enter}

/c tells the console window to close after execution, /k lets it stay open

or you could use an COM object and even get the output.

objShell := ComObjCreate("WScript.Shell")
objExec := objShell.Exec(ComSpec " /c stop")

strStdOut := ""
while, !objExec.StdOut.AtEndOfStream
{
    strStdOut := objExec.StdOut.ReadAll()
}

Update: Without the run command at all:

SetTitleMatchMode, 2

send #r

WinWait, TITLE_OF_THE_RUN_WINDOW
WinActivate

send cmd{Enter}

WinWait, cmd.exe
WinActivate

WinGetTitle, title
Send stop{Enter}

WinWait, %title%,,, stop
WinClose,

TITLE_OF_THE_RUN_WINDOW replace this with the title of the window, which opens on Win+r. A windows cmd window has the command in its title while it gets executed. So we save the title of the command window, and wait for it to drop the command ("stop") and close it then.

UPDATE: Cmd window close added to solution 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM