Sign in to follow this  
Followers 0
mikeveli20

Need help with simple script - noob to AutoIt

7 posts in this topic

Hey all,

I have a bit of a programming background so I understand concepts and stuff like that, but I'm brand new to AutoIt. I'm trying to create a simple script that will exit a game when I press the ESC key which I'm trying to map to Alt-F4. Is this even possible? Here's what I've got so far that isn't working:

--Start

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

$executable = ""

$directory = ""

if $numParms > 0 Then

$game = $CmdLine[1]

Else

msgBox(4096,"Error","You must provide a game parameter")

exit(1)

EndIf

$var = IniReadSection("test.ini",$game)

If @error Then

MsgBox(4096, "", "Error occured, Invalid parameter or the ini file is missing.")

exit(1)

Else

$executable = IniRead("test.ini",$game,"executable","")

$directory = IniRead("test.ini",$game,"directory","")

$fullpath = $directory & $executable

$msg = ">>" & $directory & $executable & "<<"

;msgBox(4096,"Testing",$msg,15)

run($fullpath,$directory)

EndIf

_GetOut()

Func _GetOut()

send("{AltDown}{F4}")

send("{AltUp}")

EndFunc

--End

I've based the script on the code and concepts from a script found here:

http://www.a-ruud-awakening.com/arcade/ind...Site.AmLasGames

I'm trying to achieve the same effect, by exiting the game while just pressing ESC.

Any help would be great. Thanks.

Share this post


Link to post
Share on other sites



Hey all,

I have a bit of a programming background so I understand concepts and stuff like that, but I'm brand new to AutoIt. I'm trying to create a simple script that will exit a game when I press the ESC key which I'm trying to map to Alt-F4. Is this even possible? Here's what I've got so far that isn't working:

--Start

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

$executable = ""

$directory = ""

if $numParms > 0 Then

$game = $CmdLine[1]

Else

msgBox(4096,"Error","You must provide a game parameter")

exit(1)

EndIf

$var = IniReadSection("test.ini",$game)

If @error Then

MsgBox(4096, "", "Error occured, Invalid parameter or the ini file is missing.")

exit(1)

Else

$executable = IniRead("test.ini",$game,"executable","")

$directory = IniRead("test.ini",$game,"directory","")

$fullpath = $directory & $executable

$msg = ">>" & $directory & $executable & "<<"

;msgBox(4096,"Testing",$msg,15)

run($fullpath,$directory)

EndIf

_GetOut()

Func _GetOut()

send("{AltDown}{F4}")

send("{AltUp}")

EndFunc

--End

I've based the script on the code and concepts from a script found here:

http://www.a-ruud-awakening.com/arcade/ind...Site.AmLasGames

I'm trying to achieve the same effect, by exiting the game while just pressing ESC.

Any help would be great. Thanks.

Welcome to the AutoIt forums mikeveli20 :)

Looks like the only thing missing in your script is a loop to keep the script running. Another hotkey to exit the script would be useful. To send Alt and F4 you can use Send("!{F4}")

If you use code tags around your code it's easier to read. [ code ] before and [ /code ] after but without the spaces.

HotKeySet("{ESC}", "_GetOut")
HotKeySet("{F*}", "StopRun")
$executable = ""
$directory = ""
If $numParms > 0 Then
    $game = $CmdLine[1]
Else
    MsgBox(4096, "Error", "You must provide a game parameter")
    Exit (1)
EndIf
$var = IniReadSection("test.ini", $game)
If @error Then
    MsgBox(4096, "", "Error occured, Invalid parameter or the ini file is missing.")
    Exit (1)
Else
    $executable = IniRead("test.ini", $game, "executable", "")
    $directory = IniRead("test.ini", $game, "directory", "")
    $fullpath = $directory & $executable
    $msg = ">>" & $directory & $executable & "<<"
;msgBox(4096,"Testing",$msg,15)
    Run($fullpath, $directory)
EndIf

While 1
    Sleep(100)
WEnd

Func StopRun()
    Exit
EndFunc  ;==>StopRun


_GetOut()
Func _GetOut()
    Send("!{F4}")
EndFunc  ;==>_GetOut

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Worked like a charm! Thanks! The only thing is when the game exits, there is a command window open. Is there a way to close this automatically? When I run the game without the script and press Alt-F4 to exit, there is no command window. However, when the script is running and I press ESC or Alt-F4 to exit, there is a command window.

Share this post


Link to post
Share on other sites

WinClose() or WinKill() won't work for this whole thing?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

WinClose worked. Here is the final script:

$numParms=$CmdLine[0]
HotKeySet("{ESC}", "_GetOut")
HotKeySet("{F*}", "StopRun")
$executable = ""
$directory = ""
$window = ""
If $numParms > 0 Then
    $game = $CmdLine[1]
Else
    MsgBox(4096, "Error", "You must provide a game parameter")
    Exit (1)
EndIf
$var = IniReadSection("test.ini", $game)
If @error Then
    MsgBox(4096, "", "Error occured, Invalid parameter or the ini file is missing.")
    Exit (1)
Else
    $executable = IniRead("test.ini", $game, "executable", "")
    $directory = IniRead("test.ini", $game, "directory", "")
    $fullpath = $directory & $executable
    $msg = ">>" & $directory & $executable & "<<"
;msgBox(4096,"Testing",$msg,15)
    Run($fullpath, $directory)
    winwait($window)
    WinActivate($window)
EndIf

While 1
    Sleep(100)
WEnd

Func StopRun()
    Exit
EndFunc;==>StopRun


_GetOut()
Func _GetOut()
    if winexists($window) Then
;msgBox(4096,"Getting Out","Killing Window",15)
        winClose($window)
        Send("!{F4}")
    EndIf
    Exit(0)
EndFunc;==>_GetOut

Is there anything in here that is not necessary or redundant that I can eliminate to tighten up the code? I'm not sure if I used the WinClose function efficiently. It does the trick so I guess it's ok.

Edited by mikeveli20

Share this post


Link to post
Share on other sites

WinClose worked. Here is the final script:

$numParms=$CmdLine[0]
HotKeySet("{ESC}", "_GetOut")
HotKeySet("{F*}", "StopRun")
$executable = ""
$directory = ""
$window = ""
If $numParms > 0 Then
    $game = $CmdLine[1]
Else
    MsgBox(4096, "Error", "You must provide a game parameter")
    Exit (1)
EndIf
$var = IniReadSection("test.ini", $game)
If @error Then
    MsgBox(4096, "", "Error occured, Invalid parameter or the ini file is missing.")
    Exit (1)
Else
    $executable = IniRead("test.ini", $game, "executable", "")
    $directory = IniRead("test.ini", $game, "directory", "")
    $fullpath = $directory & $executable
    $msg = ">>" & $directory & $executable & "<<"
;msgBox(4096,"Testing",$msg,15)
    Run($fullpath, $directory)
    winwait($window)
    WinActivate($window)
EndIf

While 1
    Sleep(100)
WEnd

Func StopRun()
    Exit
EndFunc;==>StopRun


_GetOut()
Func _GetOut()
    if winexists($window) Then
;msgBox(4096,"Getting Out","Killing Window",15)
        winClose($window)
        Send("!{F4}")
    EndIf
    Exit(0)
EndFunc;==>_GetOut

Is there anything in here that is not necessary or redundant that I can eliminate to tighten up the code? I'm not sure if I used the WinClose function efficiently. It does the trick so I guess it's ok.

Apart from F* which I guess should be F8, it looks ok to me.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Is there anything in here that is not necessary or redundant that I can eliminate to tighten up the code? I'm not sure if I used the WinClose function efficiently. It does the trick so I guess it's ok.

I think Send("!{F4}") is not neccessary after WinClose($window) anymore.

WinClose($window) does the same job as Send("!{F4}").

BTW: Nice first script ;-)

Edited by Zedna

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