[SOLVED] How to implement "AutoEdit" in VirtualTreeView? lazarus / 31-LCL ---------------------------------------------------------------------- mtrsoft · 2019-06-06 19:00 System: Win7 /32 Lazarus: v2.0.2 virtualtreeview: v5.5.3-R1 I am using the VTV in "grid mode" and trying to have an alphanumeric key press do two things: 1. Create and start the Edit control. -- This works. 2. Automatically enter the alphanumeric character into the edit control without having to press the key a second time. -- This is not working. I have tried four code variations, none of which have accomplished my goal #2. In the following KeyDown event code look at the statements close to the "ERROR" comments. 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 TPgCtrlChildVST1Grid.vstKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);Var  ANode      :PVirtualNode;  OldNode    :PVirtualNode;  I          :Integer;  AKey       :Word;  AShift     :TShiftState;  SavedKey   :Word;  SavedShift :TShiftState;  AMessage :TLMessage;beginSavedKey := 0;SavedShift := [];If (NOT TVirtualStringTree(Sender).IsEditing) then  begin  If b_GridEnterAdvances and (Key = vk_Return) and (Shift = [])    then  begin          {Code to move to the next column when the Return key is pressed}                    end    else  begin          If b_GridAtoZStartsEditing             AND             (               ((Shift = []) and (Char(Key) in ['0'..'9', 'A'..'Z']))               OR               ((Shift = [ssShift]) and (Char(Key) in ['A'..'Z']))             )            then  begin                  (*****                {This invokes the Edit control but does NOT enter the character into the Edit control}                  If NOT TVirtualStringTree(Sender).IsEditing then                    begin                  AKey := VK_F2;                    AShift := [];                  vstKeyDown(Sender, AKey, AShift);                    end;                *****)                   (*******)                  {Save the key, enter edit mode then ? re-post/re-send key to the edit control}                  SavedKey := Key;                  SavedShift := Shift;                  Key := 0; {So that nothing is done when this procedure exits unless Key is set to its original value.}                 {Start editing the Node then try to re-send the                 same character / message to the editor.}                   If TVirtualStringTree(Sender).EditNode(                      TVirtualStringTree(Sender).FocusedNode                      , TVirtualStringTree(Sender).FocusedColumn) then                    begin                     {After the editor is started pass the original key through.}                    //Key := SavedKey;                  //Shift := SavedShift;                    {                  NO ERROR, BUT the character is NOT entered into the editor / edit control.                    }                     {After the editor is started post original message the the VST.}                    //PostMessage(TVirtualStringTree(Sender).Handle, LM_KEYDOWN, SavedKey, ShiftStateToKeys(SavedShift));                    {                  NO ERROR, BUT the character is NOT entered into the editor / edit control.                    Debugger indicates the VirtualStringTree does not have a "Handle" property,                    BUT VST is a TCustomControl descendant which is a TWinControl descendant so it should have a Handle propery.                    }                   {After editor started have the EditLink process the original message.}                    //AMessage.Msg := LM_KEYDOWN;                  //AMessage.WParam := SavedKey;                    //AMessage.LParam := ShiftStateToKeys(SavedShift);                    //TmtrsVTELBase(TVirtualStringTree(Sender).EditLink).ProcessMessage(AM essage);                    {                    ERROR IS: "External SIGSEGV", which occurs in the EditLink's                    ProcessMessages procedure shown below.                    procedure TmtrsVTELBase.ProcessMessage(var Message: TLMessage); stdcall;                  begin                    >>>>>> FEdit.WindowProc(Message); <<<<<<<                    end;                    }                   {After editor started post original message the the Editor / Edit control.}                    //PostMessage(TmtrsVTELBase(TVirtualStrin gTree(Sender).EditLink).Edit.Handle, LM_KEYDOWN, SavedKey, ShiftStateToKeys(SavedShift));                    {                  ERROR IS: "External SIGSEGV"                    }                     {In all four cases above the Editlink and the Edit have been                  assigned valid values after the EditNode procedure is executed.}                     end;                  (******)                end;          end;  end; //Inherited; end;  Does anyone have a suggestion as to how I can resolve this? ---------------------------------------------------------------------- jamie · 2019-06-07 00:36 You can add to or set the initial text in an edit control.. MyEditControl.Text := My_VK_CODE; ---------------------------------------------------------------------- mtrsoft · 2019-06-07 05:38 Unfortunately, trying to assign text to the edit control results in the same SIGSEGV error. Although the edit control seems to have a valid value, the debugger indicates that its memory location is not accessible. This seems odd to me because when I DON'T try to re-send the character or assign a value to its text property I can then edit its contents in the normal way. ---------------------------------------------------------------------- jamie · 2019-06-07 21:10 I don't understand, it should work. You first ensure the control is created then you can add text to it... You must be over looking something. Either that you need to do this in a KeyUp instead. ---------------------------------------------------------------------- mtrsoft · 2019-06-09 17:00 I have not been able to determine why the Edit control's memory is not accessible and referencing its methods or properties results in a SIGSEGV error. HOWEVER, I have found a way to make it work by using the "MouseAndKeyInput" unit that comes with Lazarus. (In the Windows version of Lazarus its located at C:\Lazarus\components\mouseandkeyinput\).) I have included several code snippets below that illustrate the solution. Things to note about the implementation: 1. The SendAKey procedure MUST be a form method. 2. Besides starting the editing process by pressing an alphanumeric key and then sending that key to the edit control I also want to send an Alt+Down keypress instead of the original key if the edit control has a dropdown, such as a DateTimePicker. Because I was not able to determine the type of the edit control at runtime I decided to use a variable, SendAKeyDataAltDownCols, to identify the grid columns that use such an edit control. The values in this variable should be set when the form is created or whenever the VirtualStringTree's column count is changed. 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 {The form definition}TPgCtrlChildVST1Grid = class(TPgCtrlChildForm)..  public  { Public declarations }    SendAKeyDataKey   :Word;    SendAKeyDataShift :TShiftState;    SendAKeyDataAltDownCols :Set Of Byte;    Procedure SendAKey( Data :PtrInt);    Procedure DoSendAKey( AKey :Word; AShift :TShiftState);...end;  implementation uses...  , MouseAndKeyInput {Located in c:\lazarus\components\mouseandkeyinput}  ;  Var  SavedEditKey         :Word;  SavedEditShift       :TShiftState;  Procedure TPgCtrlChildVST1Grid.SendAKey( Data :PtrInt);BeginIf (SendAKeyDataShift <> []) then KeyInput.Apply( SendAKeyDataShift);KeyInput.Press(SendAKeyDataKey);If (SendAKeyDataShift <> []) then KeyInput.UnApply( SendAKeyDataShift);end; Procedure TPgCtrlChildVST1Grid.DoSendAKey( AKey :Word; AShift :TShiftState);beginSendAKeyDataKey := AKey;SendAKeyDataShift := AShift;Application.QueueAsyncCall(@SendAKey, 0);end;  procedure TPgCtrlChildVST1Grid.vstKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);Var  ANode      :PVirtualNode;  I        :Integer; begin If (NOT TVirtualStringTree(Sender).IsEditing) then  begin  If b_GridEnterAdvances and (Key = vk_Return) and (Shift = [])    then  begin                {Code to move to the next column.}              end    else  begin          If b_GridAtoZStartsEditing           AND             (               ((Shift = []) and (Char(Key) in ['0'..'9', 'A'..'Z']))               OR               ((Shift = [ssShift]) and (Char(Key) in ['A'..'Z']))             )            then  begin                                                                {Save the key, enter edit mode then simulate a second keypress}                  SavedEditKey := Key;                  SavedEditShift := Shift;                  Key := 0; {So that nothing is done when this procedure exits}                   {Start editing the Node then simulate a keypress which will be sent                   to the control that has the focus.}                  If TVirtualStringTree(Sender).EditNode(                      TVirtualStringTree(Sender).FocusedNode                      , TVirtualStringTree(Sender).FocusedColumn) then                    begin                                                                                                  {Set when vst created or number of colums changed}                    {Identifies columns that have editors with dropdowns}                    //SendAKeyDataAltDownCols := [4, 6, 7];                     If (TVirtualStringTree(Sender).FocusedColumn IN SendAKeyDataAltDownCols)                       then  DoSendAKey( VK_DOWN, [ssAlt])                     else  DoSendAKey( SavedEditKey, SavedEditShift);                                                                                                end;                                                                                                end;          end;  end;end;   I hope others find this solution useful.