Programmatically trigger an event lazarus / 21-General ---------------------------------------------------------------------- Tommi · 2025-03-26 20:44 I would like to simulate an event, for example trigger TMemo.onKeyDown. I guess I should use sendMessage but I don't really know how. Any idea? Thank you ---------------------------------------------------------------------- H₂SO₄ · 2025-03-27 06:45 I wouldn't recommend using  SendMessage  since then you'd have to deal with the nitty-gritty (platform-dependent) details of how messages are encoded. The simplest way is to just call the event handler directly: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SimulateKeyPress(Memo: TMemo);var  Key: Word = VK_W;begin  Memo.OnKey(Nil, Key, []); // Sends 'W' without any modifiersend; There exist other possibilities too; some wait for the event to complete, others just place it into the message queue and return immediately: UnitFoo.pas Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit UnitFoo; {$mode objfpc}{$H+} interface uses  Classes, Forms, StdCtrls, LMessages; type  TFormFoo = class(TForm)    MemoBar: TMemo;    procedure AsyncMemoKey(Data: PtrInt);    procedure MemoBarKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);  end; var  FormFoo: TFooForm; procedure SimulateMemoBarEvent(); implementation procedure SimulateMemoBarEvent();var  Key: Word = VK_W;begin   // Option 1: Direct call  FormFoo.MemoBarKeyDown(Nil, Key, []);   // Option 2: Indirect call  FormFoo.MemoBar.OnKeyDown(Nil, Key, []);   // Option 3: Synchronous message  SendMessage(FormFoo.MemoBar.Handle, LM_KEYDOWN, Key, $00000000);   // Option 4: Asynchronous message  PostMessage(FormFoo.MemoBar.Handle, LM_KEYDOWN, Key, $00000000);   // Option 5: Asynchronous event  Application.QueueAsyncCall(@FormFoo.AsyncMemoKey, Key);end; procedure TFormFoo.AsyncMemoKey(Data: PtrInt);var  Key: Word;begin  Key := Data;   // Any of options 1-4, e.g. option 1  MemoBar.OnKeyDown(Nil, Key, []);end; procedure TFormFoo.MemoBarKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin  // ...end; end. UnitFoo.lfm: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i object FormFoo: TFormFoo  object MemoBar: TMemo    OnKeyDown = MemoBarKeyDown  endend ---------------------------------------------------------------------- Tommi · 2025-03-28 10:24 Hi, thank you, but options 3 and 4 doesn't work (at least on my ubuntu 24.04). I attach an implementation example. If you press buttons 3 or 4 nothing happens. ---------------------------------------------------------------------- cdbc · 2025-03-28 11:14 Hi Don't forget to "SendMessage(FormFoo.MemoBar.Handle, LM_KEYUP, Key, $00000000);" Regards Benny ---------------------------------------------------------------------- wp · 2025-03-28 11:49 Install the LazMouseAndKeyInput package, it allows you to create virtual key presses and mouse clicks in a cross-platform way: https://wiki.freepascal.org/MouseAndKeyInput. Find the package in folder components/mouseandkeyinput of your Lazarus installation. ---------------------------------------------------------------------- H₂SO₄ · 2025-03-28 21:47 Quote from: Tommi on March 28, 2025, 11:24:30 amHi, thank you, but options 3 and 4 doesn't work (at least on my ubuntu 24.04). That's because I tested it on a different platform (Windows) and those options are platform-dependent . Which is why I wouldn't encourage using them unless absolutely necessary and sticking to the easier & clearer platform-agnostic options #1 and #2. Quote from: wp on March 28, 2025, 12:49:53 pmInstall the LazMouseAndKeyInput package, it allows you to create virtual key presses and mouse clicks in a cross-platform way: https://wiki.freepascal.org/MouseAndKeyInput. Find the package in folder components/mouseandkeyinput of your Lazarus installation. Note that this is only required if you want to simulate key presses and mouse clicks system-wide (outside your own program). ---------------------------------------------------------------------- Zoran · 2025-03-28 22:12 Quote from: wp on March 28, 2025, 12:49:53 pmInstall the LazMouseAndKeyInput package, it allows you to create virtual key presses and mouse clicks in a cross-platform way: https://wiki.freepascal.org/MouseAndKeyInput. Find the package in folder components/mouseandkeyinput of your Lazarus installation. This package is GPL licensed, which can be acceptable for quite a limited number of projects.