Board index » delphi » how do I use Hot Keys

how do I use Hot Keys

Hi All,

I want to use the F4 from the TEdit Component so I use when key presssed use
if key = #244 then procedure

thanks for anyone who can help

Aaron

 

Re:how do I use Hot Keys


Hi Aaron, you're almost there ....

The following code will put the text "<F4>" on the end of the current text
in Edit1.

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if Key=VK_F4 then
          Edit1.text:=Edit1.Text+'<F4>';
end;

Of course, if you want to fire off a procedure instead, just replace the
"Edit1.text..." with the name of your procedure.

Also, bear in mind that if the Form's KeyPreview property is set to True
then it will also receive the keystroke.  This can be useful for doing
hotkeys for all components on the form, eg:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if Key=VK_F4 then
        if ActiveControl is TEdit then
          TEdit(ActiveControl).Text:=TEdit(ActiveControl).Text+'<F4>';
end;

This will apply to all TEdit controls on the form.  Just remember to set the
form's KeyPreview property to True!

Hope this helps,

- Bob -

QBD The Bookshop <supp...@qbdthebookshop.com.au> wrote in message
news:pMX_4.132$292.2566@nsw.nnrp.telstra.net...

Quote
> Hi All,

> I want to use the F4 from the TEdit Component so I use when key presssed
use
> if key = #244 then procedure

> thanks for anyone who can help

> Aaron

Re:how do I use Hot Keys


The examples forget to set Key := 0 if the key is processed (otherwise F4
would still be processed, provided it had a function).

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;  Shift:
TShiftState);
begin
  if Key=VK_F4 then
  begin
    Edit1.text := Edit1.Text + '<F4>';
    Key := 0;
  end;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift:
TShiftState);
begin
  if (Key=VK_F4) and (ActiveControl is TEdit) then
  begin
    TEdit(ActiveControl).Text := TEdit(ActiveControl).Text + '<F4>';
    Key := 0;
  end;
end;

"Bob Brown" <bob.br...@opus.co.nz> schreef in bericht
news:8hht74$8j0$1@com.opus.co.nz...

Quote
> Hi Aaron, you're almost there ....

> The following code will put the text "<F4>" on the end of the current text
> in Edit1.

> procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
>   Shift: TShiftState);
> begin
>      if Key=VK_F4 then
>           Edit1.text:=Edit1.Text+'<F4>';
> end;

> Of course, if you want to fire off a procedure instead, just replace the
> "Edit1.text..." with the name of your procedure.

> Also, bear in mind that if the Form's KeyPreview property is set to True
> then it will also receive the keystroke.  This can be useful for doing
> hotkeys for all components on the form, eg:

> procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
>   Shift: TShiftState);
> begin
>      if Key=VK_F4 then
>         if ActiveControl is TEdit then
>           TEdit(ActiveControl).Text:=TEdit(ActiveControl).Text+'<F4>';
> end;

> This will apply to all TEdit controls on the form.  Just remember to set
the
> form's KeyPreview property to True!

> Hope this helps,

> - Bob -

> QBD The Bookshop <supp...@qbdthebookshop.com.au> wrote in message
> news:pMX_4.132$292.2566@nsw.nnrp.telstra.net...
> > Hi All,

> > I want to use the F4 from the TEdit Component so I use when key presssed
> use
> > if key = #244 then procedure

> > thanks for anyone who can help

> > Aaron

Re:how do I use Hot Keys


Y'know I thought of that at the time but then forgot all about it!  :)  Lets
have a bit of fun with this routine ...

Something else that is a neat trick is getting the hotkey text to be
inserted at the cursor position.  To do that, use:

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  s : string;
  save_SelStart : integer;

const
  HotText = '<F4>';
  HotKey = VK_F4;

begin
  { The SelStart property is the position of the insert point in the  }
  { control.  Note that this is only valid for controls that you can  }
  { type in.  Check the control's properties in the help just in case }

  if Key=HotKey then
  with Edit1 do
  begin
    { Save the SelStart property - setting the Text property resets it! }
    save_SelStart:=SelStart;

    { We can't work directly on Edit1.text so put it in a string var }
    s:=Text;

    { If there's text selected, delete it - this is standard windows
behaviour }
    if SelLength<>0 then System.Delete(S,SelStart+1,SelLength);

    { Insert the HotText - see const above. }
    System.Insert(HotText,S,SelStart+1);

    { Put it back in Edit1.Text }
    Text:=s;

    { Move the insert point... }
    SelStart:=save_SelStart+Length(HotText);

    { "Nullify" the keystroke and we're done! }
    Key:=0;
  end;
end;

Try that on and see what happens.  Notice that I have qualified the Insert
and Delete procedures by prepending "System." to it.  If you're working
inside a "with" block that is referring to a TTable or something else that
has valid Insert or Delete methods then the compiler will complain.

Cheers,

- Bob -

Quote
M.H. Avegaart <avega...@NOSPAMmccomm.nl> wrote in message

news:8hiahk$507$1@porthos.nl.uu.net...
Quote
> The examples forget to set Key := 0 if the key is processed (otherwise F4
> would still be processed, provided it had a function).

> procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;  Shift:
> TShiftState);
> begin
>   if Key=VK_F4 then
>   begin
>     Edit1.text := Edit1.Text + '<F4>';
>     Key := 0;
>   end;
> end;

> procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift:
> TShiftState);
> begin
>   if (Key=VK_F4) and (ActiveControl is TEdit) then
>   begin
>     TEdit(ActiveControl).Text := TEdit(ActiveControl).Text + '<F4>';
>     Key := 0;
>   end;
> end;

> "Bob Brown" <bob.br...@opus.co.nz> schreef in bericht
> news:8hht74$8j0$1@com.opus.co.nz...
> > Hi Aaron, you're almost there ....

> > The following code will put the text "<F4>" on the end of the current
text
> > in Edit1.

> > procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
> >   Shift: TShiftState);
> > begin
> >      if Key=VK_F4 then
> >           Edit1.text:=Edit1.Text+'<F4>';
> > end;

> > Of course, if you want to fire off a procedure instead, just replace the
> > "Edit1.text..." with the name of your procedure.

> > Also, bear in mind that if the Form's KeyPreview property is set to True
> > then it will also receive the keystroke.  This can be useful for doing
> > hotkeys for all components on the form, eg:

> > procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
> >   Shift: TShiftState);
> > begin
> >      if Key=VK_F4 then
> >         if ActiveControl is TEdit then
> >           TEdit(ActiveControl).Text:=TEdit(ActiveControl).Text+'<F4>';
> > end;

> > This will apply to all TEdit controls on the form.  Just remember to set
> the
> > form's KeyPreview property to True!

> > Hope this helps,

> > - Bob -

> > QBD The Bookshop <supp...@qbdthebookshop.com.au> wrote in message
> > news:pMX_4.132$292.2566@nsw.nnrp.telstra.net...
> > > Hi All,

> > > I want to use the F4 from the TEdit Component so I use when key
presssed
> > use
> > > if key = #244 then procedure

> > > thanks for anyone who can help

> > > Aaron

Other Threads