TEdit.Text already changed when KeyPress is triggered lazarus / 31-LCL ---------------------------------------------------------------------- daniel_sap · 2024-11-02 07:59 Hi, I observe different behaviour on Windows and MacOS when OnKeyPress event is triggered. On Mac when the event is triggered the Text value is already changed and contains the Key which is pressed. For example if I see in the edit the text 'Edit1' and then press the 'A' in the OnKeyPress event handler the 'A' is appended to the text. In Windows it is still the old text. Is there any way to have the old value when key is pressed. 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 Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    procedure Edit1KeyPress(Sender: TObject; var Key: char);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin  ShowMessage(Edit1.Text);end; end.      (I'm writing a custom control, so I want to handle it in the TCustomEdit descendant.) Thanks Lazarus 4.99 (rev main_4_99-369-g86db8d9f83) FPC 3.2.2 aarch64-darwin- cocoa I built Lazarus for arm64 by myself. Not sure if this is related to that behaviour ---------------------------------------------------------------------- jamie · 2024-11-02 17:03 that event requires both the KeyDown and KeyUp so yes, it will be changed already.,. Use the KeyDown even to make alterations ---------------------------------------------------------------------- Bart · 2024-11-02 17:14 It's perfectly lega to use OnKeyPress to make changes (e.g. block certain keys). Only after OnKeyPressed is processed the Text should be updated (e.g. the control should only then receive the key). This should work as expected: 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 TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin  if not (Key in ['0'..'9'] then     Key := #0  else    //just mess with the user's input for fun    Key := '9';end; The result should be that whatever you type, it's either rejected, or converted to a '9'. This can only happen because it's the altered Key that is received by the control, not the original Key. If this sample code does not work on Cocoa, then that is a bug. Bart ---------------------------------------------------------------------- jamie · 2024-11-02 18:51 Quote from: Bart on November 02, 2024, 06:14:17 pmIt's perfectly lega to use OnKeyPress to make changes (e.g. block certain keys). Only after OnKeyPressed is processed the Text should be updated (e.g. the control should only then receive the key). This should work as expected: 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 TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin  if not (Key in ['0'..'9'] then     Key := #0  else    //just mess with the user's input for fun    Key := '9';end; The result should be that whatever you type, it's either rejected, or converted to a '9'. This can only happen because it's the altered Key that is received by the control, not the original Key. If this sample code does not work on Cocoa, then that is a bug. Bart You are correct, Sorry. Jamie ---------------------------------------------------------------------- daniel_sap · 2024-11-02 20:12 QuoteIt's perfectly lega to use OnKeyPress to make changes (e.g. block certain keys). Only after OnKeyPressed is processed the Text should be updated (e.g. the control should only then receive the key). This should work as expected: Code: Pascal  [Select] procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char); begin   if not (Key in ['0'..'9'] then     Key := #0   else     //just mess with the user's input for fun     Key := '9'; end; The result should be that whatever you type, it's either rejected, or converted to a '9'. This can only happen because it's the altered Key that is received by the control, not the original Key. If this sample code does not work on Cocoa, then that is a bug. Bart This is not working on MacOS. The text is changed prior to KeyPress execution and the Key is not used after that for updating the Text. Even SelStart and SelLength are already updated 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 MainForm; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    Memo1: TMemo;    procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);    procedure Edit1KeyPress(Sender: TObject; var Key: char);    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin  Memo1.Lines.Add('Key press: ' + Edit1.Text);  if not (Key in ['0'..'9']) then    Key := #0  else    //just mess with the user's input for fun    Key := '9';  Memo1.Lines.Add(Format('Selection SelStart: %d, SelCount: %d', [Edit1.SelStart, Edit1.SelLength]));end; procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);begin  Memo1.Lines.Add('Key Up: ' + Edit1.Text);end; procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState  );begin  Memo1.Lines.Add('Key down: ' + Edit1.Text);end; end. ---------------------------------------------------------------------- Bart · 2024-11-02 21:07 @daniel_sap: Which version of Lazarus are you using? If you are not using Lazarus main (4.99) could you try that? If the issue is present on Lazarus main, then you should file a bugreport. Since I don't have access to a Mac, I cannot reporduce this issue myself. Bart ---------------------------------------------------------------------- daniel_sap · 2024-11-03 06:36 Quote@daniel_sap: Which version of Lazarus are you using? If you are not using Lazarus main (4.99) could you try that? If the issue is present on Lazarus main, then you should file a bugreport. Since I don't have access to a Mac, I cannot reporduce this issue myself. Bart I'm with Lazarus 4.99 (rev main_4_99-369-g86db8d9f83) FPC 3.2.2 aarch64-darwin-cocoa (I built Lazarus for arm64) Not latest commit. ASAP will try to pull latest changes and try again