Jump to content

Sending Commands Or Keystrokes Into A Program?


Recommended Posts

I'm very new to this, and I was wondering if it was possible to send commands or keystrokes into a program that is minimized or not the one in focus. Sorry if this question has been asked before, but I couldn't find anything with the search. Maybe I'm just bad at searching.

Link to comment
Share on other sites

I'm very new to this, and I was wondering if it was possible to send commands or keystrokes into a program that is minimized or not the one in focus. Sorry if this question has been asked before, but I couldn't find anything with the search. Maybe I'm just bad at searching.

Run("notepad.exe")
sleep(500)
   AutoItSetOption("WinTitleMatchMode", 4)
ControlSend ( "Untitled - Notepad", "","Edit1", "Test sending while minimized"&@LF)
sleep(2000)
WinSetState ( "Untitled - Notepad", "", @SW_MINIMIZE  )

ControlSend ( "Untitled - Notepad", "","Edit1", "HELLO")

sleep(500)
WinSetState ( "Untitled - Notepad", "",@SW_RESTORE  )
WinActivate("Untitled - Notepad")

  sleep(1500)
$a =  ControlCommand ( "Untitled - Notepad", "","Edit1","GetCurrentLine", "")
MsgBox(262144, "","Current line  " & $a)
ControlSend ( "Untitled - Notepad", "","Edit1", "{Right 3}")
  sleep(1500)
$a =  ControlCommand ( "Untitled - Notepad", "","Edit1","GetLine", 1)
MsgBox(262144, "", $a)
$a =  ControlCommand ( "Untitled - Notepad", "","Edit1","GetLine", 2)
MsgBox(262144, "", $a)
ControlClick ( "Untitled - Notepad", "" , "Edit1","left" )

ecc all in C:\Programmi\Autoit3\AutoIt3.chm >>>> ControlCommand

Edited by Lapo
Link to comment
Share on other sites

Dang, I still can't get it to work. I'm trying to send a message in game while having it minimized.

Something like sending every minute or so. like Enter, message, Enter every 60 seconds.

I can't get anything through though.

Something like ControlSend("game name", "","", "{Enter}") should work to send the enter right?

Only thing that i can think of is that the name if the window is incorrect.

Link to comment
Share on other sites

Dang, I still can't get it to work. I'm trying to send a message in game while having it minimized.

Something like sending every minute or so. like Enter, message, Enter every 60 seconds.

I can't get anything through though.

Something like ControlSend("game name", "","", "{Enter}") should work to send the enter right?

Only thing that i can think of is that the name if the window is incorrect.

you are right

ControlSend ( "title", "text", controlID, "string" [, flag]

use C:\PROGRA~1\AUTOIT3\AU3INFO.EXE and get the EXACT game Caption

EDIT:

flag [optional] Changes how "keys" is processed:

flag = 0 (default), Text contains special characters like + to indicate SHIFT and {LEFT}

to indicate left arrow.

flag = 1, keys are sent raw.

Edit 2 :

ControlFocus

--------------------------------------------------------------------------------

Sets input focus to a given control on a window.

ControlFocus ( "title", "text", controlID

ControlSend

--------------------------------------------------------------------------------

Sends a string of characters to a control.

ControlSend ( "title", "text", controlID, "string" [, flag] )

PS:

are you sending to a Windows or to a Comtrol ?

IE: Notepad is a windows Notepad's edit a control

Edited by Lapo
Link to comment
Share on other sites

sleep(2000)
; get Active Window  (Title)(Text)
; Author - SmOke_N & Valuater

$MyText = WinGetText(FindActiveWindow())
$MyTitle = WinGetTitle(FindActiveWindow())
MsgBox(0, $MyTitle, $MyText)
Func FindActiveWindow()
    Local $HidOpt = Opt("WinDetectHiddenText", 1);0=don't detect, 1=do detect
    Local $w_WinList = WinList()
    For $i = 1 to $w_WinList[0][0]
      If $w_WinList[$i][0] <> "" AND BitAnd(WinGetState($w_WinList[$i][1]), 2) _
          And BitAnd(WinGetState($w_WinList[$i][1]), 8) Then Return $w_WinList[$i][0]
      Next
    Opt("WinDetectHiddenText", $HidOpt);0=don't detect, 1=do detect
EndFunc

generic sample on get set title:

Run("calc.exe")

   AutoItSetOption("WinTitleMatchMode", 4)
    WinWaitActive( "Calculator", "", 5 )
    WinSetTitle ( "Calculator", "", "newtitle" )
 ControlFocus ( "newtitle","" , " Button9" )
 ControlClick ( "newtitle", "" , "Button9","left",5 )
  ControlClick ( "newtitle", "" , "Button7","left",5 )
; use right instead left
Link to comment
Share on other sites

I don't get the whole window or control thing. All i can tell you is that it's a D3D program that has been minimized.

Actually, I just tried that title edit thing, and it edited the title of it, so not I don't know why it doesn't send into the program.

>>>>>>>>>>>> Window Details <<<<<<<<<<<<<

Title: RF

Class: D3D Window

Size: X: -32000 Y: -32000 W: 160 H: 31

That's after renaming it. Don't know if that'll help. Edited by ExZeRoEx
Link to comment
Share on other sites

I don't get the whole window or control thing. All i can tell you is that it's a D3D program that has been minimized.

Actually, I just tried that title edit thing, and it edited the title of it, so not I don't know why it doesn't send into the program.

$a =Run("notepad.exe")

MsgBox(262144, "", $a)

$PID = ProcessExists("notepad.exe") ; Will return the PID or 0 if the process isn't found.

MsgBox(262144, "", $PID)

If ProcessExists("notepad.exe") Then

MsgBox(0, "Example", "Notepad is running.")

EndIf

Link to comment
Share on other sites

I think I figured out why it doesn't work. It seems that when the game is minimized it goes pretty much completely idle. No CPU usage at all. I guess I'm not able to send commands to it like I wanted to. Thanks for you help though.

Link to comment
Share on other sites

I got a PID of 2016 off of the program, don't really know what I'm supposed to do with it.

I must go now I have no time to test it :

$i_PID = Run("notepad.exe")
MsgBox(262144, "", $i_PID)

$a2 =_ProcessGetName( $i_PID )
MsgBox(262144, "", @error)
MsgBox(262144, "",$a2)
Func _ProcessGetName( $i_PID )
    If Not ProcessExists($i_PID) Then
        SetError(1)
        Return ''
    EndIf
    Local $a_Processes = ProcessList()
    If Not @error Then
        For $i = 1 To $a_Processes[0][0]
            If $a_Processes[$i][1] = $i_PID Then Return $a_Processes[$i][0]
        Next
    EndIf
    SetError(1)
    Return ''
EndFunc

; Description - Returns a string containing the process name that belongs to a given PID.

; Syntax - _ProcessGetName( $iPID )

; Parameters - $iPID - The PID of a currently running process

; Requirements - None.

; Return Values - Success - The name of the process

; Failure - Blank string and sets @error

; 1 - Process doesn't exist

; 2 - Error getting process list

; 3 - No processes found

; Author(s) - Erifash <erifash [at] gmail [dot] com>, Wouter van Kesteren.

; Notes - Supplementary to ProcessExists().

Link to comment
Share on other sites

I think I figured out why it doesn't work. It seems that when the game is minimized it goes pretty much completely idle. No CPU usage at all. I guess I'm not able to send commands to it like I wanted to. Thanks for you help though.

OK .. ciao

Link to comment
Share on other sites

Last thing :

I'm not confident with games but if you search here is a lot of code for

Have you tried it in this way

air code :

while 1
    sleep(60 * 1000); loops for 1 minute
ControlFocus("RF", "", "D3D Window")
;WARNING .. only if D3D Window is class in au3info
; like this sample with Notepad :
;Control ID: 15; you may use also the contol ID
;Class: Edit1
;Text: 
ControlSend("RF", "","D3D Window","{ENTER}")
;$a = ControlGetFocus("RF")
;MsgBox(262144, "", $a)
;ControlGetFocus 
;Returns the ControlRef# of the control that has keyboard focus within a specified window.

wend

notepad sample :

run("notepad")
sleep(500)
while 1
    sleep(60 * 10)
ControlFocus("Untitled - Notepad", "", "Edit1")

ControlSend("Untitled - Notepad", "","Edit1","a{ENTER}")

wend
Edited by Lapo
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...