Wednesday, January 5, 2011

Making the Enter key work like a Tab

Here is something I picked up off Compuserve that should help.  I
have a zip that contains examples of several other controls besides 
the edit.  If anyone would like a copy of the zip, let me know and I 
will send it to you.

Using the <Enter> key like a <Tab> key with Delphi Controls
The example code supplied here demonstrates how to trap the 
<Enter> key and the cursor keys to provide better data entry
The trick is to overide the Keypress and KeyDown events so
that they process the keys the way you want. In the examples
supplied I have used the <Enter> key to move to the next 
control (like the <Tab> key) and the cursor Up and Down keys
to move to the previous and next controls respectively.
The Edit and EBEdit use the cursor keys as stated above, but
the Combobox and the Listbox use Shift-Up and Shift-Down 
instead so as not to interfere with existing functionality.
The Grid control uses the <Enter> key to move between fields,however it will not move from the last field of the last row.It is very easy to make it exit the grid at this point if you
need to.
The method used to move to the next/previous control is the 
Windows API call SendMessage which is used to dispatch a 
WM_NEXTDLGCTL to the form the controls are children to. Delphi provides a function called GetParentForm to get the 
handle of the parent form of the control.
These simple extensions can be expanded to respond to almost
any keyboard event, and I think using this method is less 
trouble than trapping keys in the forms OnKey events (using
Feel free to use the code as you wish, but if you discover 
something new please let me in on it!

Simon Callcott

CIS: 100574,1034
  Edit control that reponds as if the <Tab> key has been pressed when an
  <Enter> key is pressed, moving to the next control.
  Very simple extension to the KeyPress event, this technique should work
  with TDBedit as well, Useful for data entry type apps.
  Less trouble than using the Keypreview function of the form to do the same

  Please Use Freely.

  Simon Callcott  CIS: 100574, 1034

unit Entedit;

  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;
  TEnterEdit = class(TEdit)


    procedure KeyPress(var Key: Char); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;



procedure Register;

procedure Register;begin
  RegisterComponents('Samples', [TEnterEdit]);end;
procedure TEnterEdit.KeyPress(var Key: Char);var
   MYForm: TForm;begin

   if Key = #13 then
       MYForm := GetParentForm( Self );
       if not (MYForm = nil ) then
           SendMessage(MYForm.Handle, WM_NEXTDLGCTL, 0, 0);
       Key := #0;

   if Key <> #0 then inherited KeyPress(Key);
procedure TEnterEdit.KeyDown(var Key: Word; Shift: TShiftState);var
   MYForm: TForm;
   CtlDir: Word;begin

   if (Key = VK_UP) or (Key = VK_DOWN) then
       MYForm := GetParentForm( Self );
       if Key = VK_UP then CtlDir := 1
       else CtlDir :=0;
       if not (MYForm = nil ) then
           SendMessage(MYForm.Handle, WM_NEXTDLGCTL, CtlDir, 0);
   else inherited KeyDown(Key, Shift);

Q.  "Is there a way to use the return key for data entry, instead of tab or the
A.  Use this code for an Edit's OnKeyPress event.

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
      If Key = #13 Then
        SelectNext(Sender as tWinControl, True, True );
        Key := #0;

    This causes Enter to behave like tab.  Now, select all controls on the form
    you'd like to exhibit this behavior (not Buttons) and go to the Object
    Inspector and set their OnKeyPress handler to EditKeyPress.  Now, each
    control you selected will process Enter as Tab.  If you'd like to handle
    this at the form (as opposed to control) level, reset all the controls
    OnKeyPress properties to blank, and set the _form_'s OnKeyPress property to
    EditKeyPress.  Then, change Sender to ActiveControl and set the form's
    KeyPreview property to true:

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
      If Key = #13 Then
        SelectNext(ActiveControl as tWinControl, True, True );
        Key := #0;

    This will cause each control on the form (that can) to process Enter as Tab.

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum