Recent

Author Topic: [CLOSED] Send key presses to Process  (Read 1634 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[CLOSED] Send key presses to Process
« on: July 30, 2020, 08:41:31 am »
How do I send keypresses to a TProcess?

So far I've got this to play :-

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  9.   Windows, Process;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnSTART: TButton;
  17.     ListBox1: TListBox;
  18.     Panel1: TPanel;
  19.     Timer1: TTimer;
  20.     procedure btnSTARTClick(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure Timer1Timer(Sender: TObject);
  23.   private
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.   MyShell : TProcess;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39.  
  40. procedure TForm1.btnSTARTClick(Sender: TObject);
  41. var
  42.   cmd : string;
  43. begin
  44.    with MyShell do
  45.      begin
  46.        Options := Options + [poUsePipes];
  47.        Executable:='mpv';
  48.        ShowWindow:=swoHIDE;
  49.        Parameters.Add('--wid');
  50.        Parameters.Add(inttostr(Panel1.Handle));
  51.        Parameters.Add('d:\media\test.mkv');
  52.        Execute;
  53.      end;
  54.    Timer1.Enabled:=true;
  55. end;
  56.  
  57. procedure TForm1.FormCreate(Sender: TObject);
  58. begin
  59.   MyShell := TProcess.Create(nil);
  60. end;
  61.  
  62. procedure TForm1.Timer1Timer(Sender: TObject);
  63. var
  64.   FOutList : TStringList;
  65. begin
  66.   FOutList:=TStringList.Create;
  67.  
  68.   if MyShell.Output.NumBytesAvailable > 0 then
  69.     FOutList.LoadFromStream(MyShell.Output);
  70.  
  71.   if MyShell.Stderr.NumBytesAvailable > 0 then
  72.     FOutList.LoadFromStream(MyShell.Stderr);
  73.  
  74.   ListBox1.Items := FOutList;
  75.  
  76.   FOutList.Free;
  77. end;
  78.  
  79. end.
  80.  
  81.  

Thanks in advance.
« Last Edit: July 31, 2020, 07:50:46 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

paweld

  • Hero Member
  • *****
  • Posts: 1645
Re: Send key presses to Process
« Reply #1 on: July 30, 2020, 05:24:16 pm »
if it is a console application you can use: https://github.com/t-edson/UnTerminal
if it's a window application then you can use keybd_event on Windows OSes, e.g.
Code: Pascal  [Select][+][-]
  1. uses
  2.   Windows;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   app: HWND;
  7. begin
  8.   app := FindWindow('mpv', nil);
  9.   if app > 0 then
  10.   begin
  11.     ShowWindow(app, SW_SHOW);
  12.     SetForegroundWindow(app);
  13.     //send Ctrl + C
  14.     keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
  15.     keybd_event(Word('C'), MapVirtualKey(Word('C'), 0), 0, 0);
  16.     keybd_event(Word('C'), MapVirtualKey(Word('C'), 0), KEYEVENTF_KEYUP, 0);
  17.     keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
  18.   end;
  19. end;            

and if you are trying to create a video player then use VLC and: https://prog.olsztyn.pl/paslibvlc/
Best regards / Pozdrawiam
paweld

flowCRANE

  • Hero Member
  • *****
  • Posts: 986
Re: Send key presses to Process
« Reply #2 on: July 30, 2020, 07:07:20 pm »
Code: Pascal  [Select][+][-]
  1. keybd_event(Word('C'), MapVirtualKey(Word('C'), 0), 0, 0);

Why not VK_C? There is no need to cast a literal.

BTW: SendInput is a better solution.
Lazarus 4.6 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Send key presses to Process
« Reply #3 on: July 31, 2020, 07:48:56 am »
IMHO MPV is easier and for me  better than VLC. I tried VLC and there were many problems. MPV just works  and I have just found I can communicate via a pipe.

Anyway thanks for the help.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018