OnKeyUp and OnClick event override in custom TEdit component [SOLVED] lazarus / 31-LCL ---------------------------------------------------------------------- totya · 2016-10-15 18:38 Hi! I want to write own custom component from TEdit, 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 TMyEdit = class(TEdit) and I'd like to override default OnKeyUp, and OnMouseUp events. How can I do it? These events override need for me, because only here can I get the correct ccursor position (SelStart). Thanks! ---------------------------------------------------------------------- totya · 2016-10-15 23:27 Okay, I found myself... 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 type  TMyEdit = class (TEdit)  protected    procedure KeyUpAfterInterface (var Key: Word; Shift: TShiftState); override;    procedure MouseUp (Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;  and then... 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 TMyEdit.KeyUpAfterInterface (var Key: Word; Shift: TShiftState);begin  Inherited;   SetupVariables;end; procedure TMyEdit.MouseUp (Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);begin  Inherited;   SetupVariables;end; ---------------------------------------------------------------------- Bart · 2016-10-16 09:49 Maybe better override KeyUp()? Bart ---------------------------------------------------------------------- totya · 2016-10-16 10:13 Hi! Thanks for answer! I think no, because I need read TEdit.SelStart value after KeyUp, and the KeyUpAfterInterface event is slightly executed later than normal KeyUp. So, KeyUp is possible works almost all of time, but I think te KeyUpAfterInterface is better, because this executed later. Again, I needed own component (it is done and works) because SelStart value is completely invalid in OnKeyDown event, and if the user click or select text in TEdit, the SleStart is changed too, and I can get the new SelStart value information from the MouseUp event. If you want, see this sample, OnKeyDown SelStart value is wrong, especially if you use the left or rigth arrow key, or End or Home key. ---------------------------------------------------------------------- rvk · 2016-10-16 10:34 Quote from: totya on October 16, 2016, 12:13:30 pmI think no, because I need read TEdit.SelStart value after KeyUp, and the KeyUpAfterInterface event is slightly executed later than normal KeyUp. You could call inherited AFTER your SetupVariables, couldn't you? 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 type  TMyEdit = class (TEdit)  protected    procedure KeyUp(var Key: Word; Shift : TShiftState); override;    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;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 TMyEdit.KeyUp(var Key: Word; Shift : TShiftState);begin  SetupVariables;  Inherited; // <--- call this one last.end; procedure TMyEdit.MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);begin  Inherited;  SetupVariables;end;I think overriding KeyUp() is the most standard way. Or doesn't that work in your case?? (if you really need it after the LM_KEYUP-messaging) ---------------------------------------------------------------------- totya · 2016-10-16 10:39 Quote from: rvk on October 16, 2016, 12:34:54 pmYou could call inherited AFTER your SetupVariables, couldn't you? Hi Master! No, and no, and no. I want to read SelStart information at the least as possible, as I wrote. ---------------------------------------------------------------------- rvk · 2016-10-16 10:40 Quote from: totya on October 16, 2016, 12:39:17 pmQuote from: rvk on October 16, 2016, 12:34:54 pmYou could call inherited AFTER your SetupVariables, couldn't you?No, and no, and no. I want to read SelStart information at the least as possible, as I wrote. Ok, in case you need it after the LM_KEYUP-messaging you're overriding the correct one. ---------------------------------------------------------------------- totya · 2016-10-16 10:49 Quote from: rvk on October 16, 2016, 12:40:14 pmQuote from: totya on October 16, 2016, 12:39:17 pmQuote from: rvk on October 16, 2016, 12:34:54 pmYou could call inherited AFTER your SetupVariables, couldn't you?No, and no, and no. I want to read SelStart information at the least as possible, as I wrote. Ok, in case you need it after the LM_KEYUP-messaging you're overriding the correct one. Hi Master! KeyUpAfterInterface works correctly, but I will see what you wrote, if you write me, how can I do it (with code example), in own component. ---------------------------------------------------------------------- rvk · 2016-10-16 11:29 Quote from: totya on October 16, 2016, 12:49:56 pmKeyUpAfterInterface works correctly, but I will see what you wrote, if you write me, how can I do it (with code example), in own component. I already showed the code if you want to override keyup(). But you're correct that if you want an event after the messaging is done for keyup, you're using the correct override. Overriding keyup() in your own component is done when you want to execute code before or after the user defined onKeyUp(). (The after or before is determined by the place of inherited.) The same goes for keyup/down and nouseup/down. I'm not sure if the SelStart changes in the keyup or keydown (but you can test by checking it in the keyup event) but using the KeyUpAfterInterface is indeed the last one called. ---------------------------------------------------------------------- totya · 2016-10-16 11:45 Quote from: rvk on October 16, 2016, 01:29:56 pmI'm not sure if the SelStart changes in the keyup or keydown (but you can test by checking it in the keyup event) Thanks for the answer! See this sample from here. ---------------------------------------------------------------------- totya · 2016-10-16 13:42 Modified TEdit components, ther aren't finished version: 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 IdleTimeSelectInfoEdit; {$mode objfpc}{$H+} interface // by Totya v1.2 alpha /2016.10.16/// http://wiki.freepascal.org/LCL_Key_Handling// http://lazarus-ccr.sourceforge.net/docs/lcl/stdctrls/tedit.html uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,   LazUTF8; type  TAfterTextSelectionChangedEvent =    procedure(Sender: TObject;              const IdleTimeSelStart: word;              const IdleTimeSelLength: word;              const IdleTimeSelectedText: string) of Object; type  TIdleTimeSelectInfoEdit = class(TEdit)  public    constructor Create (AOwner: TComponent); override;   private    FIdleTimeSelStart: word;    FIdleTimeSelLength: word;    FIdleTimeSelectedText: string;  public    property IdleTimeSelStart: word read FIdleTimeSelStart;    property IdleTimeSelLength: word read FIdleTimeSelLength;    property IdleTimeSelectedText: string read FIdleTimeSelectedText write FIdleTimeSelectedText;   private    FOnAfterTextSelectionChanged: TAfterTextSelectionChangedEvent;  published    property OnAfterTextSelectionChanged: TAfterTextSelectionChangedEvent read FOnAfterTextSelectionChanged write FOnAfterTextSelectionChanged;    protected    procedure SetupVariablesAndEvents; virtual;   protected  procedure KeyUpAfterInterface (var Key: Word; Shift: TShiftState); override;    procedure MouseUp (Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;  end; procedure Register; implementation procedure Register;begin  RegisterComponents('My Stuff',[TIdleTimeSelectInfoEdit]);end; constructor TIdleTimeSelectInfoEdit.Create (AOwner: TComponent);begin  FIdleTimeSelStart:= 0;  FIdleTimeSelLength:= 0;  FIdleTimeSelectedText:= '';   Inherited;end; procedure TIdleTimeSelectInfoEdit.SetupVariablesAndEvents;begin  FIdleTimeSelStart:= SelStart;  FIdleTimeSelLength:= SelLength;  FIdleTimeSelectedText:= SelText;   if Assigned(FOnAfterTextSelectionChanged) then    FOnAfterTextSelectionChanged(Self, IdleTimeSelStart, IdleTimeSelLength, IdleTimeSelectedText);end; procedure TIdleTimeSelectInfoEdit.KeyUpAfterInterface (var Key: Word; Shift: TShiftState);begin  Inherited;   SetupVariablesAndEvents;end; procedure TIdleTimeSelectInfoEdit.MouseUp (Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);begin  Inherited;   SetupVariablesAndEvents;end; end. 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 DoubleCharsFilterEdit; {$mode objfpc}{$H+} interface // by Totya v1.3 alpha /2016.10.16/// http://wiki.freepascal.org/LCL_Key_Handling// http://lazarus-ccr.sourceforge.net/docs/lcl/stdctrls/tedit.html uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,   LazUTF8, lcltype,   IdleTimeSelectInfoEdit; type  TDoubleCharsFilterEdit  = class (TIdleTimeSelectInfoEdit)  public    constructor Create (AOwner: TComponent); override;   private    FDoubleCharsFilter: boolean;  published    property DoubleCharsFilter: boolean read FDoubleCharsFilter write FDoubleCharsFilter;   private  FDoubleCharsCaseInsensitive: boolean;  published    property DoubleCharsCaseInsensitive: boolean read FDoubleCharsCaseInsensitive write FDoubleCharsCaseInsensitive;   private    FDoubleCharsFilterList: string;  published    property DoubleCharsFilterList: string read FDoubleCharsFilterList write FDoubleCharsFilterList;   protected    procedure UTF8KeyPress (var UTF8Key: TUTF8Char); override;  end; procedure Register; implementation procedure Register;begin  RegisterComponents('My Stuff',[TDoubleCharsFilterEdit]);end; constructor TDoubleCharsFilterEdit.Create (AOwner: TComponent);begin  FDoubleCharsFilter:= true;  FDoubleCharsCaseInsensitive:= true;  FDoubleCharsFilterList:= '';   Inherited;end; procedure TDoubleCharsFilterEdit.UTF8KeyPress (var UTF8Key: TUTF8Char);var  i: integer;  UTF8PressedKey, UTF8CheckedKey: TUTF8Char;  UTF8Text: string;begin  Inherited;   UTF8PressedKey:= UTF8Key;  if DoubleCharsCaseInsensitive then    UTF8PressedKey:= UTF8LowerCase(UTF8PressedKey);   UTF8Text:= Text;  if DoubleCharsCaseInsensitive then    UTF8Text:= UTF8LowerCase(UTF8Text);   if DoubleCharsFilter then    if UTF8Length(DoubleCharsFilterList) > 0 then      if UTF8Length(UTF8Text)> 0 then        for i:= 1 to UTF8Length(DoubleCharsFilterList) do          begin            UTF8CheckedKey:= UTF8Copy(DoubleCharsFilterList, i, 1);            if DoubleCharsCaseInsensitive then              UTF8CheckedKey:= UTF8LowerCase(UTF8CheckedKey);             if UTF8PressedKey = UTF8CheckedKey then              if (UTF8Copy(UTF8Text, SelStart, 1) = UTF8CheckedKey) // Before cursor...                  or               (UTF8Copy(UTF8Text, SelStart +1 +SelLength, 1) = UTF8CheckedKey) // After select...                then UTF8Key:= '';          end;end; end. ---------------------------------------------------------------------- rvk · 2016-10-16 16:29 Quote from: totya on October 16, 2016, 03:42:48 pmCode: 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 TAccurateSelectInfoEdit.SetupVariablesAndEvents;begin  FAccurateSelStart:= SelStart;  FAccurateSelLength:= SelLength;   FAccurateSelectedText:= UTF8Copy(Text, AccurateSelStart +1, AccurateSelLength);   if Assigned(FOnAfterTextSelectionChanged) then  FOnAfterTextSelectionChanged(Self, AccurateSelStart, AccurateSelLength, AccurateSelectedText);end; I haven't tested this but isn't the highlighted line above the same as the highlighted line below: 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   FAccurateSelStart:= SelStart;  FAccurateSelLength:= SelLength;  FAccurateSelectedText:= SelText; Because this is the GetSelText of TEdit: 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 function TCustomEdit.GetSelText : string;begin  Result := UTF8Copy(Text, SelStart + 1, SelLength)end; You didn't really have an explanation for what these controls do but what is the reason the SelStart/SelLength/SelText doesn't work for you. You can access these always. So what makes the AccurateSelStart/AccurateSelLength/AccurateSelText different?? ---------------------------------------------------------------------- totya · 2016-10-16 18:26 Hi Master! First, thanks for the "SelText" cosmetic tip. Second, you are right, "accurate" info is only show the exact cursor position in idle time, but isn't needed really for the double char check. I modified the source.