Jump to content

Send Key To Process


Recommended Posts

ahmm.. what if i want to get the list of process working on my my and then when It found the process, it will send keys on it.

or maybe how to get the window handle of that process to Send Keys on it... please help.

Link to comment
Share on other sites

I'm having a hard time understanding the first question of yours, but for the last part, you could use something like this:

Edit: Fixed a small error in my code(whops):

#include <WinAPI.au3>

Run("notepad.exe")
ProcessWait("notepad.exe")

Dim $aNotepad = _ProcessGetWinEx("notepad.exe") ; For example to find notepad's window from it's process

ControlSend($aNotepad[1], "", "", "123abc - ControlSend{enter}") ; One way, only works on some windows(note: controlID "can" be set to "")

WinActivate($aNotepad[1])
Send("123abc - normal Send") ; Another way, if you have nothing against activating the window first.

Func _ProcessGetWinEx($ivPid, $svClass = "")
   
    $ivPid = ProcessExists($ivPid)
    If Not $ivPid Then Return(SetError(1, 0, 0))
   
    Local $avwArray = WinList()
    Local $avRet[1] = [0]
   
    For $i = 1 To $avwArray[0][0]
        If WinGetProcess($avwArray[$i][1]) = $ivPid Then
            If $svClass = "" Or _WinAPI_GetClassName($avwArray[$i][1]) = $svClass Then
                $avRet[0] += 1
                ReDim $avRet[$avRet[0]+1]
                $avRet[$avRet[0]] = $avwArray[$i][1]
            EndIf
        EndIf
    Next
   
    Return $avRet
   
EndFunc
Edited by FreeFry
Link to comment
Share on other sites

Sorry, I noticed I made an error in the first code I posted(I forgot that my _ProcessGetWinEx function returns an array of windows(if the process should happen to own more windows than one)).

I fixed that code, and I'm not sure ControlSend works for hidden windows, but the normal Send does, if you give focus to the window first.

Example:

#include <WinAPI.au3>

Run("notepad.exe", "", @SW_HIDE)
ProcessWait("notepad.exe")

Dim $aNotepad = _ProcessGetWinEx("notepad.exe") ; For example to find notepad's window from it's process
ControlSend($aNotepad[1], "", "", "123abc - ControlSend{enter}") ; This does not work
WinActivate($aNotepad[1])
Send("123abc - normal Send") ; This does work though
WinSetState($aNotepad[1], "", @SW_SHOW)

Func _ProcessGetWinEx($ivPid, $svClass = "")
   
    $ivPid = ProcessExists($ivPid)
    If Not $ivPid Then Return(SetError(1, 0, 0))
   
    Local $avwArray = WinList()
    Local $avRet[1] = [0]
   
    For $i = 1 To $avwArray[0][0]
        If WinGetProcess($avwArray[$i][1]) = $ivPid Then
            If $svClass = "" Or _WinAPI_GetClassName($avwArray[$i][1]) = $svClass Then
                $avRet[0] += 1
                ReDim $avRet[$avRet[0]+1]
                $avRet[$avRet[0]] = $avwArray[$i][1]
            EndIf
        EndIf
    Next
   
    Return $avRet
   
EndFunc
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...