Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
[CLOSED] Send key presses to Process
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
You can embed Windows Con...
by
Thaddy
[
Today
at 10:02:09 am]
[Reopened] TSaveDialog
by
Zvoni
[
Today
at 09:54:15 am]
[New Component] ExtTabCtr...
by
ovidio
[
Today
at 09:50:44 am]
FPC Unleashed (inline var...
by
Martin_fr
[
Today
at 09:37:00 am]
Codepage issue in console...
by
Hartmut
[
Today
at 09:28:33 am]
new JPEG XL in pure Pasca...
by
Tomxe
[
Today
at 08:02:53 am]
Sudden errors
by
egsuh
[
Today
at 06:16:42 am]
Conscious Artificial Inte...
by
schuler
[
Today
at 02:42:53 am]
Implementing an Elo ratin...
by
mas steindorff
[
Today
at 02:22:34 am]
Can /my/ AI help me with ...
by
microxa
[
Today
at 12:56:29 am]
IndySecOpenSSL is now ava...
by
TheMouseAUS
[
Today
at 12:08:51 am]
RunFormula: math expressi...
by
stormray
[June 17, 2026, 10:32:05 pm]
Pdf Viewer in Pascal
by
Tomxe
[June 17, 2026, 07:18:20 pm]
Canvas size
by
Thaddy
[June 17, 2026, 07:15:50 pm]
What am I missing here? [...
by
Thaddy
[June 17, 2026, 03:39:02 pm]
Questions about 16 color ...
by
wp
[June 17, 2026, 01:45:07 pm]
TCHATGPT — An Artificial ...
by
Weiss
[June 17, 2026, 07:00:13 am]
RFC: Separation of MCU an...
by
ackarwow
[June 16, 2026, 11:06:14 pm]
Error with last fixes_3.2...
by
patyit
[June 16, 2026, 09:49:02 pm]
Mundo Medieval 3D MMORPG ...
by
Rodrigo Robles
[June 16, 2026, 06:06:17 pm]
Which Control should I us...
by
wp
[June 16, 2026, 05:08:55 pm]
Just Question App paramet...
by
eldonfsr
[June 16, 2026, 04:50:19 pm]
Content is distorting / w...
by
Handoko
[June 16, 2026, 03:53:02 pm]
ZxTune chiptunes player
by
Guva
[June 16, 2026, 12:41:14 pm]
Onscroll event for Tscrol...
by
laz_one_or2
[June 16, 2026, 11:16:39 am]
« previous
next »
Print
Pages: [
1
]
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]
[+]
[-]
unit
Unit1
;
{$mode objfpc}{$H+}
interface
uses
Classes
,
SysUtils
,
Forms
,
Controls
,
Graphics
,
Dialogs
,
StdCtrls
,
ExtCtrls
,
Windows
,
Process
;
type
{ TForm1 }
TForm1
=
class
(
TForm
)
btnSTART
:
TButton
;
ListBox1
:
TListBox
;
Panel1
:
TPanel
;
Timer1
:
TTimer
;
procedure
btnSTARTClick
(
Sender
:
TObject
)
;
procedure
FormCreate
(
Sender
:
TObject
)
;
procedure
Timer1Timer
(
Sender
:
TObject
)
;
private
public
end
;
var
Form1
:
TForm1
;
MyShell
:
TProcess
;
implementation
{$R *.lfm}
{ TForm1 }
procedure
TForm1
.
btnSTARTClick
(
Sender
:
TObject
)
;
var
cmd
:
string
;
begin
with
MyShell
do
begin
Options
:
=
Options
+
[
poUsePipes
]
;
Executable
:
=
'mpv'
;
ShowWindow
:
=
swoHIDE
;
Parameters
.
Add
(
'--wid'
)
;
Parameters
.
Add
(
inttostr
(
Panel1
.
Handle
)
)
;
Parameters
.
Add
(
'd:\media\test.mkv'
)
;
Execute
;
end
;
Timer1
.
Enabled
:
=
true
;
end
;
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
begin
MyShell
:
=
TProcess
.
Create
(
nil
)
;
end
;
procedure
TForm1
.
Timer1Timer
(
Sender
:
TObject
)
;
var
FOutList
:
TStringList
;
begin
FOutList
:
=
TStringList
.
Create
;
if
MyShell
.
Output
.
NumBytesAvailable
>
0
then
FOutList
.
LoadFromStream
(
MyShell
.
Output
)
;
if
MyShell
.
Stderr
.
NumBytesAvailable
>
0
then
FOutList
.
LoadFromStream
(
MyShell
.
Stderr
)
;
ListBox1
.
Items
:
=
FOutList
;
FOutList
.
Free
;
end
;
end
.
Thanks in advance.
«
Last Edit: July 31, 2020, 07:50:46 am by pcurtis
»
Logged
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]
[+]
[-]
uses
Windows
;
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
var
app
:
HWND
;
begin
app
:
=
FindWindow
(
'mpv'
,
nil
)
;
if
app >
0
then
begin
ShowWindow
(
app
,
SW_SHOW
)
;
SetForegroundWindow
(
app
)
;
//send Ctrl + C
keybd_event
(
VK_CONTROL
,
MapVirtualKey
(
VK_CONTROL
,
0
)
,
0
,
0
)
;
keybd_event
(
Word
(
'C'
)
,
MapVirtualKey
(
Word
(
'C'
)
,
0
)
,
0
,
0
)
;
keybd_event
(
Word
(
'C'
)
,
MapVirtualKey
(
Word
(
'C'
)
,
0
)
,
KEYEVENTF_KEYUP
,
0
)
;
keybd_event
(
VK_CONTROL
,
MapVirtualKey
(
VK_CONTROL
,
0
)
,
KEYEVENTF_KEYUP
,
0
)
;
end
;
end
;
and if you are trying to create a video player then use VLC and:
https://prog.olsztyn.pl/paslibvlc/
Logged
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 »
Quote from: paweld on July 30, 2020, 05:24:16 pm
Code: Pascal
[Select]
[+]
[-]
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.
Logged
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.
Logged
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
[CLOSED] Send key presses to Process
TinyPortal
© 2005-2018