<div dir="ltr">I'd like to control an external application from my Lazarus code.<br>I wrote a test applet which controls notepad.<br>Following instructions found online, I added package LazMouseAndKeyboardInput to my project and unit MouseAndKeyInput to my uses clause.<br><br>This code - to my surprise - does not work: the notepad window is shown, but no text is written into it:<br><br>procedure TForm1.btNotepadClick(Sender: TObject);<br>var<br>   app: HWND;<br>begin<br>   WinExec('notepad.exe', SW_NORMAL);<br>   app := FindWindow(nil,'senza nome - blocco note di windows');<br>   if app = 0 then<br>      exit;<br>   sleep(100);<br>   ShowWindow(app, SW_SHOW);<br>   KeyInput.Press('some text');<br>end;<br><br>I found an alternate code online but it works too much :)<br>I end with multiple (usually 3) windows of notepad open, where an invisible hand has written 'some text' into each of them.<br><br>procedure SendKeys(Buffer:String);<br>var<br>  I: Integer;<br>  W: Word;<br>begin<br>  For I := 1 to Length(Buffer) do<br>  begin<br>    W := VkKeyScan(Buffer[i]);<br>    keybd_event(w,0,0,0);<br>    keybd_event(w,0,KEYEVENTF_KEYUP,0);<br>  end;<br>end;<br><br>procedure TForm1.btNotepadClick(Sender: TObject);<br>var<br>   app: HWND;<br>begin<br>   WinExec('notepad.exe', SW_NORMAL);<br>   app := FindWindow(nil,'senza nome - blocco note di windows');<br>   if app = 0 then<br>      exit;<br>   sleep(100);<br>   ShowWindow(app, SW_SHOW);<br>   sleep(100);<br>   SendKeys('some text');<br>end;<br><br>Has somebody a fix for my code?<br><br>Probably it is not important, but when I compile my applet, I get the following warnings:<br><br>Messages, Hints: 12<br>Note: Duplicate unit "winmouseinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\winmouseinput.ppu"<br>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"<br>Note: Duplicate unit "winkeyinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\winkeyinput.ppu"<br>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"<br>Note: Duplicate unit "mouseinputintf" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\mouseinputintf.ppu"<br>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"<br>Note: Duplicate unit "mouseandkeyinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\mouseandkeyinput.ppu"<br>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"<br>Note: Duplicate unit "lazmouseandkeyinput" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\lazmouseandkeyinput.ppu"<br>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"<br>Note: Duplicate unit "keyinputintf" in "p201827", orphaned ppu "C:\lazarus_projects\automation\lib\i386-win32\keyinputintf.ppu"<br>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"<br><br>Why?<br><div><br></div><div>Thank you</div><div><br></div><div>Peppe</div></div>