Jump to content

StdinWrite - send arrow keys to child


Recommended Posts

Hope this small piece of code that can help you:

Global $hwnd = "C:\WINDOWS\system32\cmd.exe"
Global $iEr = Run(@ComSpec & " /k " & '@echo Please wait', "", @SW_SHOW)
Global $iCountW = 30

While WinExists($hwnd)
    $iCountW -= 1
    If $iCountW < 1 Then Exit
    Sleep(500)
WEnd
Sleep(500)

_SendToCmd("mkdir 1" & @CR)
_SendToCmd("rmdir 1" & @CR)
_SendToCmd("mkdir 2" & @CR)
_SendToCmd("rmdir 2" & @CR)
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd(@CR) ; {ENTER} = @CR)
_SendToCmd("exit"& @CR)

Func _SendToCmd($key)
    Sleep(250)
    WinActivate($hwnd, "")
    WinActive($hwnd, "")
    Return Send($key)
EndFunc   ;==>_SendToCmd

 

Regards,
 

Link to comment
Share on other sites

Link to comment
Share on other sites

1 hour ago, Nine said:

Shouldn't WinActive be WinWaitActive instead ?

 

@Jos, i need help. I tested ! 

WinWaitActive

The script will stop by the time set in the timeout variable,
After the time is up the script checks for the existence of the window.
I do not understand !!! I don't know if it's a bug or not!

from: AutoIt (3.3.15.3)   SciTE.exe (4.4.6.0)  Keyboard:00000409  OS:WIN_10/2009  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4

 

Edited by VIP

Regards,
 

Link to comment
Share on other sites

  • Developers

Which part do you not understand about the timeout?

The TimeOut is the Max waiting time,  so the WinWaitActive() will either wait until that Window is active and continue or else wait the number of seconds defined in the TimeOut when it is greater than 0.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@VIP  There is a couple of bugs in your code (I noticed when testing it).  Here a functional version of your code :

#include <Constants.au3>

Global $hwnd = "C:\Windows\system32\cmd.exe"
Global $iEr = Run(@ComSpec & " /k " & '@echo Please wait', "", @SW_SHOW)
Global $iCountW = 30

While Not WinExists($hwnd)
    $iCountW -= 1
    If $iCountW < 1 Then Exit MsgBox ($MB_SYSTEMMODAL, "", "Unable to lauch DOS console")
    Sleep(500)
WEnd
Sleep(500)

_SendToCmd("echo 1" & @CR)
_SendToCmd("echo 2" & @CR)
_SendToCmd("echo 3" & @CR)
_SendToCmd("echo 4" & @CR)
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd(@CR) ; {ENTER} = @CR)
_SendToCmd("exit"& @CR)

Func _SendToCmd($key)
    Sleep(250)
    WinActivate($hwnd, "")
    Local $t = WinWaitActive($hwnd, "", 2)
    ConsoleWrite ($t & @CRLF)
    Return Send($key)
EndFunc   ;==>_SendToCmd

1. Remember that using title, it is case sensitive

2. Your loop was checking the inverse

 

Link to comment
Share on other sites

Thanks Nine

I didn't really notice that WinWaitActive is case sensitive.
Thank you for the guidance.

Global $hwnd = @ComSpec
Global $iEr = Run(@ComSpec & " /k " & '@echo Please wait', "", @SW_SHOW)
Global $iCountW = 30

Global $hwndX=WinWaitActive($hwnd,'',$iCountW)
ConsoleWrite("Handle:"&$hwndX &" Error:"& @error & " Extended:"&@extended&"" &@CRLF)
#cs
While WinExists($hwnd)
    $iCountW -= 1
    If $iCountW < 1 Then Exit
    Sleep(500)
WEnd
#ce

Sleep(500)
_SendToCmd("@echo 1" & @CR)
_SendToCmd("@echo 2" & @CR)
_SendToCmd("@echo 3" & @CR)
_SendToCmd("@echo 4" & @CR)
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd(@CR) ; {ENTER} = @CR)
_SendToCmd("exit"& @CR)

Func _SendToCmd($key)
    Sleep(250)
    WinActivate($hwnd, "")
    Return Send($key)
EndFunc   ;==>_SendToCmd

 

Regards,
 

Link to comment
Share on other sites

One final suggestion to your code.  You could use ControlSend instead of Send.  It would make your code much more robust that way.  And it would alleviate the necessity of having the Console active...

Link to comment
Share on other sites

2 minutes ago, Nine said:

No need for a controlID, just leave it blank.  It is working fine without it...:)

s

Global $hwnd = @ComSpec
Global $iEr = Run(@ComSpec & " /k " & '@echo Please wait', "", @SW_SHOW)
Global $iCountW = 30

Global $hwndX=WinWaitActive($hwnd,'',$iCountW)
ConsoleWrite("Handle:"&$hwndX &" Error:"& @error & " Extended:"&@extended&"" &@CRLF)
#cs
While WinExists($hwnd)
    $iCountW -= 1
    If $iCountW < 1 Then Exit
    Sleep(500)
WEnd
#ce

Sleep(500)
_SendToCmd("@echo 1" & @CR)
_SendToCmd("@echo 2" & @CR)
_SendToCmd("@echo 3" & @CR)
_SendToCmd("@echo 4" & @CR)
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd("{UP}")
_SendToCmd(@CR) ; {ENTER} = @CR)
_SendToCmd("exit"& @CR)

Func _SendToCmd($key)
    Sleep(250)
    WinActivate($hwnd, "")
    ;Return Send($key)
    Return ControlSend($hwnd,'','','',$key)
EndFunc   ;==>_SendToCmd

Why did the script not work, where did I go wrong?

Regards,
 

Link to comment
Share on other sites

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Don't worry about this remark.  DOS console accept messages very well.  As a proof :

#include <Constants.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>

Global $hwnd = "C:\Windows\system32\cmd.exe"
Global $iEr = Run(@ComSpec & " /k " & '@echo Please wait', "", @SW_SHOW)
Global $iCountW = 30

While Not WinExists($hwnd)
    $iCountW -= 1
    If $iCountW < 1 Then Exit MsgBox ($MB_SYSTEMMODAL, "", "Unable to lauch DOS console")
    Sleep(500)
WEnd
Sleep(500)

$hwnd = WinGetHandle($hwnd)

_SendMessage($hwnd, $WM_CHAR, 0X41, 0)
_SendMessage($hwnd, $WM_CHAR, 0X42, 0)
_SendMessage($hwnd, $WM_CHAR, 0X43, 0)

 

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