Capture Key press of user

Good Day

I got a grid on my form, but what I want to do is when the user type a 3 digit code e.g 301 or 901 from his keybord and press enter, then I want to seacrh a record in my grid and display the record.

Can someone show me a example on how to capture the key stroke on the form when the user type 301 and press enter. When the user press enter, then I must do my part pf searching the grid and try and locate the record.

Now if the type 307 again and press enter then it must do my search again.

thanx
henryreynoldsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

2266180Commented:
1) set the forms keypreview property to true.
2) put a string grid on the form (not necessary for this demo :) )
3) run the below code

not that the label is there so that you can see that when pressing enter, you see what you search for. you can delete the label and it will still work as long as you replace
label1.caption:=s;
 with your find function.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;
 
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    s:string;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  s:='';
end;
 
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    label1.caption:=s;// this is the line where you do the actual search. 
    s:='';
  end else
    s:=s+key;
end;
 
end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
henryreynoldsAuthor Commented:
Hi Ciuly

Thank you very much, it is perfect.

Keep well

Henry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.