In my Delphi programs, sometimes it is necessary to simulate a keypress in TEdit that has not received the focus.
Example:
procedure TFormPegaProdutos.DBGrid1KeyPress(Sender: TObject;
var Key: Char);
begin
if not (key in ['0'..'9','/',#13,#27,'A'..'z']) Then key:=#0;
if key=#27 then close;
if (key in ['A'..'Z']) or (key in ['a'..'z']) Then
begin
PanelPesquisa.Top:=80;
PanelPesquisa.Left:=160;
PanelPesquisa.visible:=True;
Palavra1.setfocus;
if (key in ['a'..'z']) Then Key:=chr(trunc(ord(Key))-32);
keybd_event(trunc(Ord(Key)),0,0,0);
end;
if key in ['0'..'9'] Then
begin
PanelVenda.Top:=80;
PanelVenda.Left:=60;
PanelVenda.visible:=True;
ValorUni.Text:=FormatCurr('#0.00',ProTEMPP_VIS.AsCurrency);
QTD.setfocus;
keybd_event(trunc(Ord(Key)),0,0,0);
end;
...That is, the list of items (DBGrid1) the User may open a search panel for a new item without leaving the list, and can still register a sale of the item without leaving the list, which is, say By the way, very practical.
But Lazarus did not have available in the keybd_event, then how to solve this problem?
I created a simple procedure:
procedure keybd_event_on(EditControl:TEdit; var key: char);
begin
EditControl.Text:=key+' ';
EditControl.setfocus;
EditControl.SelStart:=1;
EditControl.SelLength:=1;
end;
Ready!
Now I can continue porting my programs for carrying Lazarus.
Not because of a keybd_event that the project will stop!
procedure TFormPegaProdutos.DBGrid1KeyPress(Sender: TObject;
var Key: Char);
begin
if not (key in ['0'..'9','/',#13,#27,'A'..'z']) Then key:=#0;
if key=#27 then close;
if (key in ['A'..'Z']) or (key in ['a'..'z']) Then
begin
. PanelPesquisa.Top:=80;
. PanelPesquisa.Left:=160;
. PanelPesquisa.visible:=True;
. Palavra1.setfocus;
. if (key in ['a'..'z']) Then Key:=chr(trunc(ord(Key))-32);
. keybd_event(trunc(Ord(Key)),0,0,0);
. keybd_event_on(Palavra1,Key);
end;
if key in ['0'..'9'] Then
begin
. PanelVenda.Top:=80;
. PanelVenda.Left:=60;
. PanelVenda.visible:=True;
. ValorUni.Text:=FormatCurr('#0.00',ProTEMPP_VIS.AsCurrency);
. QTD.setfocus;
. keybd_event_on(QTD,Key);
end;
The Lazarus is not 100% compatible with Delphi, but running into problems like this, we can not give up, always have an exit.
Be free!
Use lazarus / freepascal.
Sem comentários:
Enviar um comentário
Obtigado pelo comentário.