Board index » delphi » Simulating key presses

Simulating key presses

I was wondering if anyone knows how to simulate a user key press.  Basicly,
what I am trying to do is remap the return key to simulate a tab key.  I have
a form with multiple DBEdit components and I want a user to be able to
traverse these controls by pressing return (by default Delphi does this when
you press the tab key).  Also, is there a way to change the cursor in a DBEdit
component so that it is a solid block instead of a vertical line?  Any help is
appreciated.

Jennifer Cassaro
Northwestern University, Evanston, IL.   USA
[email protected]

 

Re:Simulating key presses


Quote
[email protected] (Jennifer Cassaro) wrote:
>I was wondering if anyone knows how to simulate a user key press.  Basicly,
>what I am trying to do is remap the return key to simulate a tab key.  I have
>a form with multiple DBEdit components and I want a user to be able to
>traverse these controls by pressing return (by default Delphi does this when
>you press the tab key).  Also, is there a way to change the cursor in a DBEdit
>component so that it is a solid block instead of a vertical line?  Any help is
>appreciated.

if you trap the keydown, keyup, and keypress events, you can change
what key gets processed, like so:

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_Return then Key := vk_Tab;
end;

procedure TForm1.StringGrid1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_Return then Key := vk_Tab;
end;

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then Key := #9;
end;

Hope that helps,
Matt Cowley
[email protected]

Re:Simulating key presses


Quote
[email protected] (Jennifer Cassaro) wrote:
>I was wondering if anyone knows how to simulate a user key press.  Basicly,
>what I am trying to do is remap the return key to simulate a tab key.  I have
>a form with multiple DBEdit components and I want a user to be able to
>traverse these controls by pressing return (by default Delphi does this when
>you press the tab key).  Also, is there a way to change the cursor in a DBEdit
>component so that it is a solid block instead of a vertical line?  Any help is
>appreciated.

Use the OnKeyDown event, test for the enter key and substitute the tab
key.  I'm pretty sure you need to enable the KeyPreview property in
order to do this.

Re:Simulating key presses


In ldelphi.zip there is the code to do this, and a custom component
with this built in...

ldelphi.zip or Lloyds help file is a 1.something meg file full of
code snippets and components.  It is a mix between a .hlp file and
a FAQ.  The latest file can be found Borland's Delphi support page
and should be updated every month.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
William Fisher                             [email protected]
Delphi Support
- -  -                                                  -  - -
The views expressed are mine, not Borland's;  And we both like
it that way!

Re:Simulating key presses


Hey , I [email protected] wrote:

Quote

> [email protected] (Jennifer Cassaro) wrote:

> >I was wondering if anyone knows how to simulate a user key press.  Basicly,
> >what I am trying to do is remap the return key to simulate a tab key.  I have
> >a form with multiple DBEdit components and I want a user to be able to
> >traverse these controls by pressing return (by default Delphi does this when
> >you press the tab key).  Also, is there a way to change the cursor in a DBEdit
> >component so that it is a solid block instead of a vertical line?  Any help is
> >appreciated.

> Use the OnKeyDown event, test for the enter key and substitute the tab
> key.  I'm pretty sure you need to enable the KeyPreview property in
> order to do this.

Hey I never thought of substituting the code, that's neat.

I did it by capturing the Enter key in OnKeyDown and using a function
called SelectNext as in
        SelectNext((Sender as TWinControl), True, True);

Look it up you can go forward or back based on Shift Key as in

      if Key = VK_Return then
        if Shift = [] then
           SelectNext((Sender as TWinControl), True, True)
        else if Shift = [ssShift] then
           SelectNext((Sender as TWinControl), False, True);

DJ

Other Threads