Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2024-10

automated testing code

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

duilio foschi

I'd like to control an external application from my Lazarus code. I wrote a test applet which controls notepad.
Following instructions found online, I added package LazMouseAndKeyboardInput to my project and unit MouseAndKeyInput to my uses clause.

This code - to my surprise - does not work: the notepad window is shown, but no text is written into it:

procedure TForm1.btNotepadClick(Sender: TObject);
var
   app: HWND;
begin
   WinExec('notepad.exe', SW_NORMAL);
   app := FindWindow(nil,'senza nome - blocco note di windows');
   if app = 0 then
      exit;
   sleep(100);
   ShowWindow(app, SW_SHOW);
   KeyInput.Press('some text');
end;

I found an alternate code online but it works too much :) I end with multiple (usually 3) windows of notepad open, where an invisible hand has written 'some text' into each of them.

procedure SendKeys(Buffer:String);
var
  I: Integer;
  W: Word;
begin
  For I := 1 to Length(Buffer) do
  begin
    W := VkKeyScan(Buffer[i]);
    keybd_event(w,0,0,0);
    keybd_event(w,0,KEYEVENTF_KEYUP,0);
  end;
end;
procedure TForm1.btNotepadClick(Sender: TObject);
var
   app: HWND;
begin
   WinExec('notepad.exe', SW_NORMAL);
   app := FindWindow(nil,'senza nome - blocco note di windows');
   if app = 0 then
      exit;
   sleep(100);
   ShowWindow(app, SW_SHOW);
   sleep(100);
   SendKeys('some text');
end;

Has somebody a fix for my code?

Probably it is not important, but when I compile my applet, I get the following warnings:

Messages, Hints: 12
Note: Duplicate unit "winmouseinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\winmouseinput.ppu" Note: Duplicate unit "winmouseinput" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\winmouseinput.ppu", source="C:\lazarus\components\mouseandkeyinput\winmouseinput.pas" Note: Duplicate unit "winkeyinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\winkeyinput.ppu" Note: Duplicate unit "winkeyinput" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\winkeyinput.ppu", source="C:\lazarus\components\mouseandkeyinput\winkeyinput.pas" Note: Duplicate unit "mouseinputintf" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\mouseinputintf.ppu" Note: Duplicate unit "mouseinputintf" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\mouseinputintf.ppu", source="C:\lazarus\components\mouseandkeyinput\mouseinputintf.pas" Note: Duplicate unit "mouseandkeyinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\mouseandkeyinput.ppu" Note: Duplicate unit "mouseandkeyinput" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\mouseandkeyinput.ppu", source="C:\lazarus\components\mouseandkeyinput\mouseandkeyinput.pas" Note: Duplicate unit "lazmouseandkeyinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\lazmouseandkeyinput.ppu" Note: Duplicate unit "lazmouseandkeyinput" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\lazmouseandkeyinput.ppu", source="C:\lazarus\components\mouseandkeyinput\lazmouseandkeyinput.pas" Note: Duplicate unit "keyinputintf" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\keyinputintf.ppu" Note: Duplicate unit "keyinputintf" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\keyinputintf.ppu", source="C:\lazarus\components\mouseandkeyinput\keyinputintf.pas"

Why?

Thank you

Peppe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20241027/59b29d95/attachment.htm>

Reply by email

Mattias Gaertner
to duilio foschi
duilio foschi wrote 10 lines
[...] Probably it is not important, but when I compile my applet, I get the following warnings: Messages, Hints: 12 Note: Duplicate unit "winmouseinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\winmouseinput.ppu" Note: Duplicate unit "winmouseinput" in "LazMouseAndKeyInput 0.9", ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\winmouseinput.ppu", source="C:\lazarus\components\mouseandkeyinput\winmouseinput.pas"

Maybe you added the unit path C:\lazarus\components\mouseandkeyinput\ to your project. Don't. Adding the package to your project is enough. Then clean up your project output directory and rebuild.

Mattias

Reply by email

duilio foschi
to Mattias Gaertner

Hi Mattias,

thanks to your suggestions, the results improved, but they are not what I expected.

After polishing my code ('clean up your project output directory and rebuild'), the warnings disappeared and now only one instance of notepad is created.

In the instance, the weird text
/-5 07:36 28/10/2024507:36 28/10/2024
is written.

The good part is: date and time are correct :)
The bad part is: I was expecting the string 'some text' to be written :)

The mistery thickens :)

procedure TForm1.btNotepadClick(Sender: TObject);
var
   app: HWND;
begin
   WinExec('notepad.exe', SW_NORMAL);
   app := FindWindow(nil,'senza nome - blocco note di windows');
   if app = 0 then
      exit;
   sleep(500);
   KeyInput.Press('some text');
end;

Any idea?

Thank you

Peppe

On Sun, Oct 27, 2024 at 11:39?PM Mattias Gaertner via lazarus <

Show quoted text 28 lines
wrote: On 10/27/24 19:41, duilio foschi via lazarus wrote: >[...] > Probably it is not important, but when I compile my applet, I get the > following warnings: > > Messages, Hints: 12 > Note: Duplicate unit "winmouseinput" in "p201827", orphaned ppu > "C:\lazarus_projects\automation\lib\i386-win32\winmouseinput.ppu" > Note: Duplicate unit "winmouseinput" in "LazMouseAndKeyInput 0.9", > ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\winmouseinput.ppu", > source="C:\lazarus\components\mouseandkeyinput\winmouseinput.pas" Maybe you added the unit path C:\lazarus\components\mouseandkeyinput\ to your project. Don't. Adding the package to your project is enough. Then clean up your project output directory and rebuild. Mattias -- _______________________________________________ lazarus mailing list https://lists.lazarus-ide.org/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20241028/33ac294e/attachment.htm>

Reply by email

Alexander Hofmann
to duilio foschi

Hi,

my two cents:

duilio foschi wrote
In the instance, the weird text /-5 07:36 28/10/2024507:36 28/10/2024 is written.

Inserting current date and time is a long-lasting feature of notepad (unchanged since at least Windows 2 :-)) - triggered by [F5] key (or the combination [Alt]-[E]-[D]).

Maybe, for some reason, the wrong scancodes are sent to the Application or some modifiers are active?

You may try things like
https://www.nirsoft.net/utils/keyboard_state_view.html to find out..

Best regards,

Alex

Reply by email

duilio foschi
to Alexander Hofmann

Hi Alex,

I found a different code online that works ok.

The tool you suggested is very interesting.

Thank you

Peppe

On Fri, Nov 1, 2024 at 2:51?AM Alexander Hofmann via lazarus <

Show quoted text 32 lines
wrote: Hi, my two cents: Am 28.10.24 um 15:40 schrieb duilio foschi via lazarus: > In the instance, the weird text > /-5 07:36 28/10/2024507:36 28/10/2024 > is written. Inserting current date and time is a long-lasting feature of notepad (unchanged since at least Windows 2 :-)) - triggered by [F5] key (or the combination [Alt]-[E]-[D]). Maybe, for some reason, the wrong scancodes are sent to the Application or some modifiers are active? You may try things like https://www.nirsoft.net/utils/keyboard_state_view.html to find out.. Best regards, Alex -- _______________________________________________ lazarus mailing list https://lists.lazarus-ide.org/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20241101/48613596/attachment.htm>

Reply by email