Ver Mensaje Individual
  #1  
Antiguo 04-01-2020
MaxiDucoli MaxiDucoli is offline
Miembro
 
Registrado: feb 2006
Posts: 134
Reputación: 19
MaxiDucoli Va por buen camino
Cerrar mi aplicación de consola con ESCAPE o DOS BOTONES DE JOYSTICK

Hola a todos.
FELIZ AÑO!!!!!!
Les quería hacer una consulta.
Les voy a pasar mi código y quisiera saber si hay alguna manera de cerrar el proceso de una función con la tecla ESCAPE o una combinación de dos botones de algún joystick


Código Delphi [-]
program RLCExe;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, StrUtils,Classes, ShellApi, windows;
var
CMD, DRIVE,  GAMECONSOLE,LauncherBAT, CONSOLEPROGRAM , FName, BatFilePath : String;
i : integer;
FBat : TextFile;



function EjecutarYEsperar( sPrograma: String; Visibilidad: Integer ): Integer;
var
  sAplicacion: array[0..512] of char;
  DirectorioActual: array[0..255] of char;
  DirectorioTrabajo: String;
  InformacionInicial: TStartupInfo;
  InformacionProceso: TProcessInformation;
  iResultado, iCodigoSalida: DWord;


begin
  StrPCopy( sAplicacion, sPrograma );
  GetDir( 0, DirectorioTrabajo );
  StrPCopy( DirectorioActual, DirectorioTrabajo );
  FillChar( InformacionInicial, Sizeof( InformacionInicial ), #0 );
  InformacionInicial.cb := Sizeof( InformacionInicial );

  InformacionInicial.dwFlags := STARTF_USESHOWWINDOW;
  InformacionInicial.wShowWindow := Visibilidad;
  CreateProcess( nil, sAplicacion, nil, nil, False,
                 CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
                 nil, nil, InformacionInicial, InformacionProceso );


  // Espera hasta que termina la ejecución
  repeat
    iCodigoSalida := WaitForSingleObject( InformacionProceso.hProcess, 1000 );
    //Application.ProcessMessages;
//    if  then break;

  until ( iCodigoSalida <> WAIT_TIMEOUT );

  GetExitCodeProcess( InformacionProceso.hProcess, iResultado );
  MessageBeep( 0 );
  CloseHandle( InformacionProceso.hProcess );
  Result := iResultado;
end;

function ExtractTextBetween(const Input, Delim1, Delim2: string): string;
var
  aPos, bPos: Integer;
begin
  result := '';
  aPos := Pos(Delim1, Input);
  if aPos > 0 then begin
    bPos := PosEx(Delim2, Input, aPos + Length(Delim1));
    if bPos > 0 then begin
      result := Copy(Input, aPos + Length(Delim1), bPos - (aPos + Length(Delim1)));
    end;
  end;
end;

function GetCommandLine : String;
var
i : integer;
CMD : String;
begin
CMD := '';

    for  i := 0 to ParamCount do
    begin
    CMD  := CMD + ' ' + ParamStr(i);
    //CMD := TRIM(CMD);
    end;

    if CMD <> '' then
    begin
        Result := CMD;
    end else
    begin
        Result := '';
    end;

end;

function ExtractGameConsole(s : string) : String;
var
cmd : String;
begin

CMD := ExtractTextBetween(s,'\roms\','\');

if CMD <> '' then
begin
  Result := CMD;
end else
begin
Result := '';
end;


end;

procedure EditOpt (SysNew : String);
var
Lst : TStringList;
DRV, SysOld : String;
i : integer;
begin

DRV := ExtractFileDrive(ParamStr(0));

Try
  Lst := TStringList.Create;
  Lst.LoadFromFile(DRV + '\RetroLife\emulators\retroarch\retroarch-core-options.cfg');

  for I := 0 to Lst.Count -1 do
    begin
           if AnsiContainsText(Lst[i],'atari800_system = "') then
           SysOld := ExtractTextBetween(Lst[i], 'atari800_system = "','"');

           if SysOld <> '' then
           begin
             Lst[i] := AnsiReplaceText(Lst[i],SysOld,SysNew);
             Break;
           end;

    end;

Finally
Lst.SaveToFile(DRV + '\RetroLife\emulators\retroarch\retroarch-core-options.cfg');
Lst.Free;
End;

end;

Function GetSystemProgram(s : String) : String;

CONST CONSOLABASE : string = '\RetroLife\emulators\';
var DRIVE : String;
begin
DRIVE := ExtractFileDrive(ParamStr(0));

if s = '3ds' then Result := DRIVE + CONSOLABASE + s + '\' + 'citra-qt.exe';

if s = 'scummvm' then Result := DRIVE + CONSOLABASE + s + '\' + 'scummvm.exe';

if s = 'wiiu' then Result := DRIVE + CONSOLABASE + s + '\' + 'Cemu.exe';

//if s = 'psp' then Result := DRIVE + CONSOLABASE + s + '\' + 'PPSSPP.exe';

if s = 'mame64' then Result := DRIVE + CONSOLABASE + s + '\' + 'mame64.exe';

if s = 'psp' then Result := DRIVE + CONSOLABASE + s + '\' + 'PPSSPPWindows64.exe';

if s = 'wii' then Result := DRIVE + '\RetroLife\emulators\wii-gamecube\Dolphin.exe';

if s = 'gamecube' then Result := DRIVE + '\RetroLife\emulators\wii-gamecube\Dolphin.exe';

if s = 'dreamcast' then Result := DRIVE + '\RetroLife\emulators\dreamcast\redream.exe';

if s = 'ps2' then Result := DRIVE + '\RetroLife\emulators\ps2\pcsx2.exe';

if s = 'amigacd32' then Result := DRIVE + '\RetroLife\emulators\amiga\System\Launcher\Windows\x86-64\fs-uae-launcher.exe';

if s = 'amigacdtv' then Result := DRIVE + '\RetroLife\emulators\amiga\System\Launcher\Windows\x86-64\fs-uae-launcher.exe';

if s = 'amiga' then Result := DRIVE + '\RetroLife\emulators\amiga\System\Launcher\Windows\x86-64\fs-uae-launcher.exe';

if s = 'daphne' then Result := DRIVE + '\RetroLife\emulators\daphne\daphne.exe';

if s = 'neogeocd' then Result := DRIVE + '\RetroLife\emulators\retroarch\retroarch.exe';

if s = 'atari800' then
begin
EditOpt('400/800 (OS B)');
Result := DRIVE + '\RetroLife\emulators\retroarch\retroarch.exe';
end;

if s = 'atari5200' then
begin
EditOpt('5200');
Result := DRIVE + '\RetroLife\emulators\retroarch\retroarch.exe';
end;

if s = 'gamecom' then Result := DRIVE + CONSOLABASE + 'mame64\mame64.exe';

if s = 'gx4000' then Result := DRIVE + CONSOLABASE + 'mame64\mame64.exe';

end;

Function GetFileName (s : String) : String;
var
i : integer;
FileName, FDrive : String;
Begin
FDrive := ExtractFileDrive(ParamStr(0));
i := POS('.exe',s);

if i <> 0  then
begin
  FileName := Copy(s,i + 5,Length(s) - i + 5);
end;

if FileName <> '' then Result := FDrive + Filename;

End;

function ReadTXT (s : String) : String;
var
F : TStringList;
a : String;
begin
//AssignFile(f,s);

//Rewrite(F);

F := TStringList.Create;
//ReadLn(F,a);

F.LoadFromFile(s);
a := f.Text;

if a <> '' then
begin
  Result := a;
end else
begin
  Result := '';
end;

F.Free;

end;

Procedure CreateBat (ConsoleGAME,ConsolePROGRAM,FileGAME : String);
var
Lst : TStringlist;
//EMUPATH,
SCUMMVMGAME, DAPHNENAME : String;
begin

Lst := TStringList.create;
Lst.Clear;

  try

    if ConsoleGAME = '3ds' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "citra-qt.exe"' + ' "' + FileGAME + '" -f');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'scummvm' then
    begin

    SCUMMVMGAME := ReadTXT(FileGAME);
    Lst.Add ('echo START');
    Lst.Add ('set HOME="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add ('set GAMEFILE="' + FileGAME + '"');
//Type %TEXTFILE%
//Lst.Add( ExtractFileDrive(ConsoleProgram));
//Lst.Add('CD ' + ExtractFilePath(ConsoleProgram));
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D ' + '"' + ExtractFilePath(ConsoleProgram) + '"'  +
    ' "scummvm.exe" -f --joystick --savepath="' + ExtractFileDrive(ConsoleProgram) + '\RetroLife\saves\scummvm"' +
    ' -p "' + ExtractFilePath(FileGAME) + '\" ' + SCUMMVMGAME);
//@echo Done
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'psp' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "PPSSPPWindows64.exe"' + ' "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'wii' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "Dolphin.exe"' + ' /b /e "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'gamecube' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "Dolphin.exe"' + ' /b /e "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'dreamcast' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "redream.exe"' + ' "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'ps2' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "pcsx2.exe"' + ' --portable --fullscreen --nogui "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'amigacd32' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "fs-uae-launcher.exe"' +
    ' "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;
//\RetroLife\emulators\amiga\System\Launcher\Windows\x86-64\fs-uae-launcher.exe
    if ConsoleGAME = 'amigacdtv' then
    begin
   Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "fs-uae-launcher.exe"' +
    ' "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'amiga' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
//    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "fs-uae-launcher.exe" ' +
    '-config:floppy-drive-count=1 "' + FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'daphne' then
    begin
    DAPHNENAME := ExtractFileName(FileGAME);
    DAPHNENAME := ChangeFileExt(DAPHNENAME,'');
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "daphne.exe" ' +
    DAPHNENAME + ' vldp -opengl -homedir "\RetroLife\roms\daphne" -fullscreen');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'neogeocd' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "retroarch.exe"' +
    ' -L "\RetroLife\emulators\retroarch\cores\fbneo_libretro.dll" --subsystem neocd "' +
    FileGAME + '"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'atari800' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "retroarch.exe"' +
    ' -L "\RetroLife\emulators\retroarch\cores\atari800_libretro.dll"' +
    ' --config "\RetroLife\emulators\retroarch\retroarch.cfg" --appendconfig "\RetroLife\emulators\retroarch\config\retroarch-override.cfg" "' +
    FileGAME + '"' + ' --log-file "\RetroLife\logs\atari800-Logfile.txt" -v');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'atari5200' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "retroarch.exe"' +
    ' -L "\RetroLife\emulators\retroarch\cores\atari800_libretro.dll"' +
    ' --config "\RetroLife\emulators\retroarch\retroarch.cfg" --appendconfig "\RetroLife\emulators\retroarch\config\retroarch-override.cfg" "' +
    FileGAME + '"' + ' --log-file "\RetroLife\logs\' + ConsoleGAME + '-Logfile.txt" -v');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'gamecom' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "mame64.exe"' +
    ' gamecom -cart1 "' +
    FileGAME + '" -v >"\RetroLife\logs\gamecom-logFile.txt"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    if ConsoleGAME = 'gx4000' then
    begin
    Lst.Add ('echo START');
    Lst.Add ('set RUTA="' + ExtractFilePath(ConsoleProgram) + '"');
    Lst.Add('START "LAUNCHIMG" /B /D "\RetroLife" "FrmClose.exe" "\RetroLife\LaunchIMGS\' + ConsoleGAME + '.jpg"');
    Lst.Add('START "RUN-EMU" /W /B /D "' + ExtractFilePath(ConsoleProgram)+ '"' + ' "mame64.exe"' +
    ' gx4000 -cart "' +
    FileGAME + '" -v >"\RetroLife\logs\gamecom-logFile.txt"');
    Lst.Add('del %0');
    Lst.Add('@echo Done');
    LauncherBAT := 'Launcher' + ConsoleGAME + '.bat';
    end;

    //--amiga-model=CD32 --cdrom-drive-0=
  finally
  Lst.SaveToFile(ExtractFilePath(ConsoleProgram) +  LauncherBAT);
  Lst.Free;
  end;
  end;



begin

  try
    { TODO -oUser -cConsole Main : Insert code here }


    CMD := GetCommandLine;
    GAMECONSOLE := ExtractGameConsole(CMD);
    CONSOLEPROGRAM := GetSystemProgram(GAMECONSOLE);
    FName := GetFileName(CMD);
    CreateBat(GAMECONSOLE,CONSOLEPROGRAM,FName);

    BatFilePath := ExtractFilePath(CONSOLEPROGRAM) + LauncherBAT;
    //ShellExecute(0,nil,PWideChar(BatFilePath),nil,nil,SW_HIDE);
    ejecutaryesperar(BatFilePath,SW_HIDE);
     {unhook the keyboard interception}


  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;


end.

Yo creo que se debería poner algo en EjecutarYEsperar para cortar el proceso, pero no sé qué ni como.
Lo que hace este programa es agarrar los parámetros de una linea de comandos y prepara la línea de comandos del emulador que corresponda, una vez hecha la linea de comandos la guarda en un archivo BAT y luego con EjecutarYEsperar corre ese BAT y hasta que no termina, el programa de consola no se cierra.

Mientras el programa de consola está de fondo esperando, se ejecuta un emulador y es éste el que mantiene el foco todo el tiempo hasta que se cierra.


Es posible poner algún código para cerrar este proceso con ESCAPE o dos botones de Joysticks???

Muchas gracias!
Responder Con Cita