Glad to see you got your .bat file working. Here is ahk code in case you are curious.
#Persistent
mameEXE := A_ScriptDir "\mame64.exe" ;path to mame64.exe
RunWait, %mameEXE%,,, MAME_PID ;RunWait will wait until the program finishes before continuing
Exitapp
Joy9::
KeyWait, Joy9, T2 ;wait 2 seconds for release key
if (ErrorLevel) ;more than 2 sec have passed
Process, Close, %MAME_PID% ;close MAME using the Process ID we got from the RunWait command
return