I thought that if someone would like to also use libxdo, I should extend this info by how to pass valid WindowId to the calls I shared.
Manually, when using xdotool from terminal, I usually find window id with "wmctrl -l" command. Wmctrl is not installed in Ubuntu by default, so: $ sudo apt-get install wmctrl.
"wmctrl -l" will list all the windows currently managed by the x11 window manager, and the very first column will be window id. The Id is a number, wmctrl prints it in hex. The format used is to prefix hex number with "0x", but Object Pascal uses '$' as default hex prefix. So, for example, "0xfefe" in Object Pascal would be:
const
C_HEX_VALUE = $fefe; //0xfefe
also, the last column of "wmctrl -l" output is the window's title. So I wrote a function that can return window's id by window's title:
(...)
uses
(...), Process, (...);
Procedure grep(var s: string; const grepstr: string);
implementation
Procedure grep(var s: string; const grepstr: string);
var
SL : TStringList;
begin
if(grepstr='')then
begin
s := '';
exit;
end;
SL := TStringList.Create;
try
SL.Text := s; s := '';
while(SL.Count>0)do
begin
if Pos(grepstr,SL[0])>0 then s := s+SL[0]+sLineBreak;
SL.Delete(0);
end;
finally
FreeAndNil(SL);
s := trim(s);
end;
end;
(...)
Procedure TForm1.cmdButtonClick(Sender: TObject);
var
WindowId : longint;
s,c : string;
p : integer;
begin
if not Process.RunCommand('wmctrl',['-l'],s)then exit;
grep(s,'VTE Terminal');
{ because the window I wanted the Id of has that title, you should use whatever applies to your case }
p := Pos(' ',s);
if(p>0)
then s := trim(Copy(s,1,p-1))
else s := '';
if Copy(s,1,2)='0x'
then Delete(s,1,2)
else exit;
WindowId := StrToIntDef('$'+s,0);
if(WindowId<=0)then exit;
writeln('WindowId='+s);
while(c<>'')do
begin
libXDO.SendKey(WindowId, Copy(c,1,1));
Delete(c,1,1);
end;
libXDO.SendKey(WindowId, 'KP_Enter');}
end;
just remember that this depends on wmctrl being installed on the system, which it is not by default on Ubuntu. That's just one way though, you could use x11 APIs from lazarus, but this way was faster for me.
I once found lazarus units that have functions for listing windows and their Id, so it is definitely possible not to depend on wmctrl, but there it is, as a reference.