changing focus on a form lazarus / 1-Linux ---------------------------------------------------------------------- bayparinc · 2009-06-23 04:06 Hi to all, I have this scenario, a Form with a 3 TEdit, TMemo, 2 TButtons and 1 TBitbtn.  When the form is   displayed, the input focus is setting on edit box 1. What I want to do is put a code on the event of tbitbtn to change the focus from one control to the other, as if i have pressed the tab key from the keyboard.  I have use      SelectNext(Sender as TWinControl, True, True );  // but doesn't do the job have tried also   PerformTab( True );  //but does not work either also DoKeyPress( VK_Tab );  doesn't work also Is their any other solution that i can i try?  Many thanks ---------------------------------------------------------------------- Vincent Snijders · 2009-06-23 09:03 Why do you pass the sender to SelectNext? Is the control you want to select then next in the taborder after the clicked bitbtn? If not, SelectNext(ActiveControl,true, true); may work better. ---------------------------------------------------------------------- bayparinc · 2009-06-24 01:14 yes, i want to pass the control to next taborder after the clicked on bitbtn, better solution if it will work for other keys also. I have tried SelectNext( ActiveControl, true, true); but doesn't do the job too.  procedure TForm1.BitBtn1Click(Sender: TObject); begin     SelectNext( ActiveControl, true, true );  // this one jumps into one of the control but not the next      taborder on the first clicked only    //SelectNext(Sender as TWinControl, True, True );  // but doesn't do the job    //PerformTab( True );  //but does not work either    //DoKeyPress( VK_Tab );  doesn't work also end; I'm trying the postmessage that works with delphi but doesn't compile in lazarus maybe because it's not a cross platform. ---------------------------------------------------------------------- clauslack · 2009-06-24 02:25 ENTER key simulates TAB key betwen TEdit controls. You can adapt this to your needs. Code: [Select]Form1.KeyPreview:=true; Code: [Select]procedure TForm1.FormKeyPress(Sender: TObject; var Key: char); begin    if key=#13 then    if (ActiveControl is TEdit) then    begin         key:=#0;         SelectNext(ActiveControl,true, true);     end; end;          Regards ---------------------------------------------------------------------- bayparinc · 2009-06-24 02:58 the application that i'm writing is actually a touch screen, possibility that there will be no keyboard.  So i'm attempting to mimic a tab or enter key after clicking the bitbtn.  SelectNext(ActiveControl, true, true); works fine using keyboard. Thanks for the help. ---------------------------------------------------------------------- bayparinc · 2009-06-26 02:02 I'm still looking for a way in which i can force my program to drive a keypress using buttons.  I'm doing a program with a numeric tbuttons and 3 tedit boxes, a tab button and esc button in form.  If any of the numeric buttons is pressed, it will be an input to the tedit box depending on the focus.  Tab button will move the focus on the next tedit, while esc button will close the form.  Initial focus will be tedit1 when the form show. I'm thinking that if my user click those buttons it will push a keypress to the keyboard buffer so that it can be trap using the  keypreview := true and keypress which will be much easier to trap. Upto now i don't have a lack of solving it:-( Any suggestions will be much appreciated. Thanks again. ---------------------------------------------------------------------- Leledumbo · 2009-06-26 07:37 QuoteI'm still looking for a way in which i can force my program to drive a keypress using buttons.You can call OnKeyPress from button's OnClick, for instance: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var   c: Char = #13; begin   Form1KeyPress(Sender,c); // send enter key end; ---------------------------------------------------------------------- bayparinc · 2009-06-26 10:49 Quote from: Leledumbo on June 26, 2009, 09:37:03 amQuoteI'm still looking for a way in which i can force my program to drive a keypress using buttons.You can call OnKeyPress from button's OnClick, for instance: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var   c: Char = #13; begin   Form1KeyPress(Sender,c); // send enter key end; Thanks Leledumbo,    Form1.KeyPress( c ); works:-) But i face another challenge, i need the focus to stay on my edit box when clicking buttons. I think the focus is given on the clicked button, so the KeyPress which is passed by the onclick event is not processed/loss. Hope someone had work on this before. ---------------------------------------------------------------------- Leledumbo · 2009-06-26 11:57 QuoteBut i face another challenge, i need the focus to stay on my edit box when clicking buttons.Add Code: [Select]Edit1.SetFocus; // or any of the TEdit somewhere in the button's OnClick. ---------------------------------------------------------------------- bayparinc · 2009-06-29 01:10 Thanks again leledumbo for your prompt reply. Sorry, this one is i think a basic in programming, but it seems i don't know how to make it.  Basically the keypress are not posted on the edit entry, another thing is how to know where is the focus entry? Thanks for your tips, it is much appreciated. Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,   StdCtrls, Buttons; type   { TForm1 }   TForm1 = class(TForm)     btn2: TBitBtn;     btn3: TBitBtn;     btnEnter: TBitBtn;     btnTab: TBitBtn;     btnEsc: TBitBtn;     btn1: TBitBtn;     Edit1: TEdit;     Edit2: TEdit;     Edit3: TEdit;     procedure btn1Click(Sender: TObject);     procedure btn2Click(Sender: TObject);     procedure btn3Click(Sender: TObject);     procedure btnTabClick(Sender: TObject);     procedure btnEscClick(Sender: TObject);     procedure FormKeyPress(Sender: TObject; var Key: char);     procedure FormShow(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation { TForm1 } procedure TForm1.btn1Click(Sender: TObject); var c:char; begin   c:= '1';   Edit1.SetFocus;       //currently hardcoded to be in edit1; enhancement is to know who has the focus when a keypress is done   Form1.Keypress(c);    // it doesn't work, it does not post the char in my edit box ;-( end; procedure TForm1.btn2Click(Sender: TObject); var c:char; begin   c:= '2';   Edit1.SetFocus;         //currently hardcoded to be in edit1; enhancement is to know who has the focus when a keypress is done   Form1.Keypress(c);    // it doesn't work, it does not post the char in my edit box ;-( end; procedure TForm1.btn3Click(Sender: TObject); var c:char; begin   c:= '3';   Edit1.SetFocus;     //currently hardcoded to be in edit1; enhancement is to know who has the focus when a keypress is done   Form1.Keypress(c);     // it doesn't work, it does not post the char in my edit box ;-( end; procedure TForm1.btnTabClick(Sender: TObject); var c:char; begin   c:=chr(9);               // should simulate a tab key   Form1.Keypress(c);       // end; procedure TForm1.btnEscClick(Sender: TObject); var c:char; begin   c:=#27;   Form1.Keypress(c);              // this one work; the form close end; procedure TForm1.FormKeyPress(Sender: TObject; var Key: char); begin   if Key = #27 then close ;   ShowMessage('Key=' + Key);   // shows correct as specified on my keypress; but the key is not posted on the edit box end; procedure TForm1.FormShow(Sender: TObject); begin   Edit1.SetFocus; end; initialization   {$I unit1.lrs} end. ---------------------------------------------------------------------- Leledumbo · 2009-06-29 09:16 QuoteBasically the keypress are not posted on the edit entryWell... that's because you're calling the form's OnKeyPress instead of the edit's one. You can try the following code (untested): Code: [Select]procedure TForm1.Btn1Click(Sender: TObject); var   c: Char; begin   c:='1';   Edit1.OnKeyPress(Edit1,c); end; procedure TForm1.Btn2Click(Sender: TObject); var   c: Char; begin   c:='2';   Edit1.OnKeyPress(Edit1,c); end; procedure TForm1.Btn3Click(Sender: TObject); var   c: Char; begin   c:='3';   Edit1.OnKeyPress(Edit1,c); end; Quoteanother thing is how to know where is the focus entry?You can get it from ActiveControl property. ---------------------------------------------------------------------- bayparinc · 2009-06-30 00:26 the code also failed, it gives me "External: SIGSEGV" and exception error "RunError(216)". ---------------------------------------------------------------------- Leledumbo · 2009-06-30 09:58 Quotethe code also failed, it gives me "External: SIGSEGV" and exception error "RunError(216)"OK, I thought I did it wrong, the Edit's OnKeyPress must be nil because it's not set. That's the cause of GPF (i.e. RTE 216). Try this (still untested): Code: [Select]procedure TForm1.Btn1Click(Sender: TObject); begin   if ActiveControl is TEdit then     ActiveControl.Text:=ActiveControl.Text+'1'; end; ---------------------------------------------------------------------- bayparinc · 2009-07-01 00:19 I got error during complilation, cause the text is not a property of active control ["identifier indents no member "text"] ---------------------------------------------------------------------- Vincent Snijders · 2009-07-01 06:44 I guess Leledumbo meant: Code: [Select]TEdit(ActiveControl).Text:=TEdit(ActiveControl).Text+'1'; ---------------------------------------------------------------------- bayparinc · 2009-07-01 06:58 I was able to solve the issue by converting the sendkey32.pas which work in delphi.  My issue now is when i compile in linux, it looks for windows.pp.  Does that mean my program only works for windows? Or is there a cross platform windows.pp equivalent? Thanks for your patience, leledumbo and vincent:-) ---------------------------------------------------------------------- Vincent Snijders · 2009-07-01 07:04 There is no cross platform equivalent for windows.pp. Some functions in the windows unit can be found in the lclintf unit. As a cross platform alternative of the sendkey32 unit, you may want to look at http://wiki.lazarus.freepascal.org/MouseAndKeyInput ---------------------------------------------------------------------- bayparinc · 2009-07-01 07:15 Thanks vincent, i'll give it a try. ---------------------------------------------------------------------- Leledumbo · 2009-07-01 08:00 QuoteI guess Leledumbo meant:Whoops! Forgot the casts, thanks Vincent (I was thinking of using as operator actually). ---------------------------------------------------------------------- Vincent Snijders · 2009-07-01 09:06 If you just tested with "is", using "as" is a waste of resources, IMHO.