Sign in to follow this  
Followers 0
Mai

Need help with 2 line codes

12 posts in this topic

hello,

I'm complately new to autoit, however I've tried to make my own script for a game, here it is:

(plz dont laugh as I just start) :)

WinWaitActive("gamewindow")

Send("{1 down}")

this is auto play, just hold key 1 and it stand and kill.

Im trying to add 1 more line to pickup drops. pickup key is " ` " or " e " and I want it pick delay every 1 sec

Can some1 help me make that pickup work ?

Share this post


Link to post
Share on other sites



WinWaitActive("gamewindow")

Send("{1 down}")
While 1
    Send('e')
    Sleep(1000)
WEnd

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here you go, Hope this helps, uses adlib so it doesn't bog the cpu down. make sure you put the right values in the ProcessExists() and WinActive() functions to match the game you are setting up.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("game trainer", 201, 136, 192, 124)
$btnGo = GUICtrlCreateButton("Go", 60, 25, 75, 25, $WS_GROUP)
$lblStatus = GUICtrlCreateLabel("", -1, 70, 196, 48, $SS_CENTER)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $bool1 = False
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnGo
            Main()

    EndSwitch
WEnd

Func Main()
    If GUICtrlRead($btnGo) = 'Go' Then
        GUICtrlSetData($btnGo, 'Stop')
        GUICtrlSetData($lblStatus, 'Running')
        AdlibEnable("GameSend",1000)
    Else
        GUICtrlSetData($btnGo, 'Go')
        GUICtrlSetData($lblStatus, '')
                $bool1 = False
        AdlibDisable()
    EndIf
EndFunc

Func GameSend()
    If Not ProcessExists("game.exe") Or Not WinActive ( "title" ) Then Return
    If Not $bool1 Then
        $bool1 = True
        Send("{1 DOWN}")
    EndIf
    Send("e")
    ;add more sends here
EndFunc
Edited by nekkutta

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

Thanks nekkutta for help, ur script is so advance for me atm, I will copy it and learn slowly.

Thanks Trung, really nice and simple script, btw I tried to add more send like this, but I doesnt work, please correct me:

WinWaitActive("gamewindow")
Send("{1 down}")
While 1
    Send('e')
    Sleep(3000)
WEnd

While 1
    Send('w')
    Sleep(15000)    
WEnd

I want to send 2 or more keys how to code it? :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I believe you need to know more about loops, such as: While..Wend, For...Next, etc.

While 1 in the above example means that the script is looping and will not get out from the loop unless you close the script. So, the While 1 after the first that has no exit point, would not be run by the script.

If you want the loop to exit (or get out from While) when the window is not active, you can use this While winActive("gamewindow")...Wend. Or you can add Exitloop inside While 1. Like I said, better to read on While...WEnd.

And since you are quite new to AutoIt, it can be problem for some to identify the correct window. So, I suggest to play around with Au3Info to get information of what window you want. Such as using Class, handle, or title of the window.

Also, do you want to create a hotkey for Down-key, or you want to send Down-key?

If you want to make a hotkey, so if you press Down-key it will do the work.. then you would need to use function IsPressed(). But, if you want the script to do the work only after the window is active, you don't need IsPressed.

To add delay to the send, you can add this option:

Opt("SendKeyDownDelay", 1000)      ;1000 millisecond = 1 sec
Edited by MDCT

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

While/Wend is for repeating a loop...

WinWaitActive("gamewindow")
Send("{1 down}")
While 1
    Send('e')
    Sleep(3000)
    Send('w')
    Sleep(15000)    
WEnd

If you want pause, just add this at the begin

HotKeySet("{ESC}", "TogglePause")

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

ESC means key Escape to pause

Edited by trung0407

Share this post


Link to post
Share on other sites

Thanks for your tips MDCT.

omg Trung, its so simple and I remeber I tried to do the same as you wrote but it didnt work :)

now it works lol. The pause function is very helpfull.

Thanks you so much!!!

Just one more question, how to keep this script working even I'm out of the window "gamewindow" ? I mean I want it keep playing when I open another window like a browser etc.

I think it's something about WinWaitActive("gamewindow")? :)

Share this post


Link to post
Share on other sites

You only need to replace the Send function with...

WinWaitActive("gamewindow")
Send("{1 down}")
While 1
    ControlSend('Title of the window or game', '', 'Control ID','e')
    Sleep(3000)
    ControlSend('Title of the window or game', '', 'Control ID','w')
    Sleep(15000)    
WEnd

where:

- title is the title or a winhandle

- control ID is the id that your window receive input stream, you can get this ID using AU3 Info tool or ControlGetFocus.

If you interest, I'll give you a program manager I made, which include everything you need to make automations for games.

Share this post


Link to post
Share on other sites

Sorry to say Trung, its not work

WinWaitActive("gamewindow")
Send("{1 down}")
While 1
    ControlSend('gamewindow', '', 'Control ID','e')
    Sleep(3000)    
WEnd

I've tried on notepad too ( ofcourse I change the title of window to the window's name )

plz check again if u dont mind?

thanks

Share this post


Link to post
Share on other sites

Try this:

if not winexists("Untitled") then 
    run ("notepad.exe")
Else
    WinActivate("Untitled")
EndIf
WinWaitActive("Untitled")
$NotepadHandle=WinGetHandle("Untitled")
Send("{1 down}")
While WinExists($NotepadHandle)
    ControlSend("[CLASS:Notepad]", '', 'Edit1','e')
    Sleep(1000)
WEnd
exit

After, just close the notepad, the script will quit automatically.

To get Edit1 or the control of the Notepad window, just like trung0407 said, you could use AU3 Info tool or script. But, you could also set it to blank, like this: ControlSend("Untitled", '', '','e') to use title or ControlSend("[CLASS:Notepad]", '', '','e') to use class.

Note: Two windows or more can have the same class or Title. But handle is unique and assigned to a window when it's created, so no two or more windows can have the same handle at the same time.

So, in above case, the script will ControlSend "e" to last active window that has class Notepad. But if you use handle, it will only Controlsend to one window.

Hope you understand a bit more about window manipulations.

Good luck.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Sorry to say Trung, its not work

WinWaitActive("gamewindow")
Send("{1 down}")
While 1
    ControlSend('gamewindow', '', 'Control ID','e')
    Sleep(3000)    
WEnd

I've tried on notepad too ( ofcourse I change the title of window to the window's name )

plz check again if u dont mind?

thanks

Because you didn't change the Control ID. With Notepad, you need to use 'Edit1', with other window you need other control id. Use AU3 info tool to see what control id your game use.

Or here is a way to get the control ID

WinActivate("title")
Sleep(3000) ;we wait 3s here so you can click where to send the key
$cID = ControlGetFocus("title") ;this function capture the control id of what you just click
ControlSend("title","", $cID, "e")

Send, ControlSend and some other functions only works if your window doesn't have protection (GameGuard or any Anti-hack is a protection)

PS: where are you from? :)

Edited by trung0407

Share this post


Link to post
Share on other sites

hi trung,how you doing?

I had vacation thats why ...

Im from Poland.

Share this post


Link to post
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
Sign in to follow this  
Followers 0