Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Help fix an AHK script to close itself if user exits a PC game via menu [SOLVED]  (Read 4799 times)

0 Members and 1 Guest are viewing this topic.

Wade007

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 309
  • Last login:April 06, 2017, 01:33:35 pm
  • A MAME cabinet at home is heaven
    • bit.ly/1cWnoIC
    • Cheapskategamer.com
I'm building the last part of my arcade cabinet; PC arcade ports.

I'm launching the executable from Mala using an AHK script so I can remap a few keys along the way.
Everything works fine except that as you know, PC games can be "quit" via an on-screen menu and not just a button combo as set up by AHK. If the user quits via the on-screen menu with the mouse then the AHK script is still left running. How can I fix this?

For example, if I launch a game called Neverball, my script is set to kill the program using the following script (ending part of script) :

~2 & ~4:: ; This is my chosen 2-button key combo for quitting all games and emulators
Process, Close, Neverball.exe
ExitAPP
return

The above (closing) script works fine, but again, if the user uses the on-screen menu to "Quit to Desktop" using the mouse instead of hitting 2 & 4 together, then AHK ends up running in the background after the PC game has quit. It never gets to the "ExitAPP" part. If I'm not careful, I can have several AHK programs running at once in the background.

Isn't there some sort of "Win-Wait Program" command that will sense if the game (Neverball.exe) has quit by itself and then shut down the AHK script and then close Auto Hot Key?

I'm sure this is an easy fix, I'm just not quite sure on the details. Thanks in advance for any help. :)


« Last Edit: July 11, 2012, 01:06:20 am by Wade007 »
My Super Arcade cabinet project:
Software blog article: http://bit.ly/1cWnoIC

Hardware blog article: http://bit.ly/1dWxjvP

YouTube video: http://bit.ly/1N818Xm

Super Arcade Cabinet Progress thread (Now Complete):
http://forum.arcadecontrols.com/index.php/topic,123292.0.html

Frontend Video Intro: http://bit.ly/1P9HPDN

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Isn't there some sort of "Win-Wait Program" command that will sense if the game (Neverball.exe) has quit by itself and then shut down the AHK script and then close Auto Hot Key?

I would use WinWaitClose. See the commands WinWaitClose and SetTitleMatchMode in the ahk help file for the relevant info.

Basically you would put something like this immediately before the hotkeys part of your script.

WinWaitClose, Neverball
exitapp

The script should exit when the Neverball game window no longer exists.

 :cheers:

Wade007

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 309
  • Last login:April 06, 2017, 01:33:35 pm
  • A MAME cabinet at home is heaven
    • bit.ly/1cWnoIC
    • Cheapskategamer.com
Thanks nitz for your quick response.

I think I correctly added in the code suggested....maybe. I must have done something wrong because it's not quite working. Could you please help me again?
The following is my full script:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance force

SetWorkingDir, C:\Arcade\PC Arcade Ports\Neverball
Run, C:\Arcade\PC Arcade Ports\Neverball\Neverball.exe
LCtrl::~LButton
LAlt::~RButton
Space::LShift
LShift::F1
z::F2
x::F3
c::ESC

a::~LButton
s::~RButton
q::LShift
w::F1
e::F2
[::F3
]::ESC

WinWaitClose, Neverball
exitAPP

~2 & ~4::
Process, Close, Neverball.exe
ExitAPP
return
« Last Edit: July 10, 2012, 01:54:48 am by Wade007 »
My Super Arcade cabinet project:
Software blog article: http://bit.ly/1cWnoIC

Hardware blog article: http://bit.ly/1dWxjvP

YouTube video: http://bit.ly/1N818Xm

Super Arcade Cabinet Progress thread (Now Complete):
http://forum.arcadecontrols.com/index.php/topic,123292.0.html

Frontend Video Intro: http://bit.ly/1P9HPDN

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Try moving all the hotkeys below the WinWaitClose command like this:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance force

SetWorkingDir, C:\Arcade\PC Arcade Ports\Neverball
Run, C:\Arcade\PC Arcade Ports\Neverball\Neverball.exe

WinWait, Neverball
WinWaitClose, Neverball
exitAPP

LCtrl::~LButton
LAlt::~RButton
Space::LShift
LShift::F1
z::F2
x::F3
c::ESC

a::~LButton
s::~RButton
q::LShift
w::F1
e::F2
[::F3
]::ESC

~2 & ~4::
Process, Close, Neverball.exe
ExitAPP
return

Anything that you want your script to automatically do (ie not tied to a hotkey) needs to go before the hotkeys.

I've also added a WinWait command so that the script waits for the window to actually exist before it waits for it not to exist - without the WinWait command, even the slightest delay between running Neverball and the window coming into existence will probably cause the script to just exit right away.

Cheers.  :cheers:

Wade007

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 309
  • Last login:April 06, 2017, 01:33:35 pm
  • A MAME cabinet at home is heaven
    • bit.ly/1cWnoIC
    • Cheapskategamer.com
Excellent nitz!! Once again...Thank you!!

Script works perfectly. You've been a great help. Thanks for your patient support and expertise.

Cheers!!  :cheers:

PS for those trying to solve this same issue: There's one thing I had to remind myself when using the WinWait and WinWaitClose commands; The next word or words on the line of code indicates the name or title of the Window, not necessarily the name of the game nor the executable name of the PC game being played. I had go into each game to adjust the settings to be NON-full screen at first so I could triple-check the Window title displayed and then plug that name in the AHK script so it would work correctly. See comment notes below regarding these correctly updated lines of AHK code. Once I entered the correct Window titles, everything worked out perfectly.

WinWait, Neverball 1.5.4; Neverball 1.5.4 is the title of the game WINDOW opening, NOT simply "Neverball"
WinWaitClose, Neverball 1.5.4 ; Neverball 1.5.4 is the title or name of the game WINDOW closing.
« Last Edit: July 11, 2012, 03:49:45 am by Wade007 »
My Super Arcade cabinet project:
Software blog article: http://bit.ly/1cWnoIC

Hardware blog article: http://bit.ly/1dWxjvP

YouTube video: http://bit.ly/1N818Xm

Super Arcade Cabinet Progress thread (Now Complete):
http://forum.arcadecontrols.com/index.php/topic,123292.0.html

Frontend Video Intro: http://bit.ly/1P9HPDN