Board index » delphi » TEdit & Enter key

TEdit & Enter key

I am using the OnKeyPress event of a TEdit component to analyze and filter
user input. I want the user to be able to provide the proper input and
use the ENTER key to move to the next field. The tab key is OK, but I
feel the ENTER key should also be capable of this function. The problem
is that for some reason, Delphi won't recognize the ENTER key as a
lagitimate key stroke. I don't know why that is. Here is a sample of
what is happening.

Procedure Edit1.OnKeyPress(Key: Char);
Begin
If Not (Key in['1'..'8',#8,#13]) Then Begin
   Key:=#0;
   End;
End.

This routine will allow the numbers 1 thru 8 and the BackSpace key, (if
pressed), to bypass this routine and go to the next routine. As far as
the numbers and the BackSpace keys are concerned, the routine works like
a charm. The #13 Character is supposed to represent the ENTER key.
However, on my system, this does not happen. I put a BreakPoint at the
begining of the routine to trap the users input at moment of keystroke.
All keys except the ENTER, TAB and a few other special keys will trip
the breakpoint. The Entter and Tab keys will not trip the breakpoint. So
Delphi is not recognizing those keys from my keyboard.

Don

 

Re:TEdit & Enter key


Donald:

Trap the Enter key at the form level using the code at the end of the message.
If you want it active just for edit controls, add:

 if ActiveControl is TEdit then

before the line

if AMessage.CharCode = VK_RETURN then begin

--
Regards
Ralph (TeamB)
(No private e-mail replies, please, unless explicitly requested).
--

Quote
Donald Borneman wrote in message <751p2e$l...@forums.borland.com>...

|I am using the OnKeyPress event of a TEdit component to analyze and filter
|user input. I want the user to be able to provide the proper input and
|use the ENTER key to move to the next field. The tab key is OK, but I
|feel the ENTER key should also be capable of this function. The problem
|is that for some reason, Delphi won't recognize the ENTER key as a
|lagitimate key stroke. I don't know why that is. Here is a sample of
|what is happening.<deletia>

unit EnterTabCM;

interface

uses
  Windows, Messages, Classes, Forms, StdCtrls, Controls;

type
  TFrmEnterTabCM = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
  private { Private-Deklarationen }
    procedure CMDialogKey(var AMessage: TCMDialogKey); Message CM_DIALOGKEY;
  public { Public-Deklarationen }
  end;

var
  FrmEnterTabCM: TFrmEnterTabCM;

implementation

{$R *.DFM}

procedure TFrmEnterTabCM.CMDialogKey(var AMessage: TCMDialogKey);
begin
    if AMessage.CharCode = VK_RETURN then begin
      if GetKeyState(VK_SHIFT) < 0 then
        SelectNext(ActiveControl, false, true)
      else
        SelectNext(ActiveControl, true, true);

      AMessage.Result := 1;
    end;

  inherited;
end;

end.

Re:TEdit & Enter key


Hi Donald,

Use the OnKeyDown event, that will work. The OnKeyPress event does not
respond to the enter key (and backspace, shift, control, tab ....).

Frans

Quote
> ----------
> Van:       Donald Borneman[SMTP:Nancy_C...@csi.com]
> Gepost op:         maandag 14 december 1998 02:12
> Gepost naar:       using
> Discussie:         TEdit & Enter key
> Onderwerp:         TEdit & Enter key

> I am using the OnKeyPress event of a TEdit component to analyze and
> filter
> user input. I want the user to be able to provide the proper input and
> use the ENTER key to move to the next field. The tab key is OK, but I
> feel the ENTER key should also be capable of this function. The
> problem
> is that for some reason, Delphi won't recognize the ENTER key as a
> lagitimate key stroke. I don't know why that is. Here is a sample of
> what is happening.

> Procedure Edit1.OnKeyPress(Key: Char);
> Begin
> If Not (Key in['1'..'8',#8,#13]) Then Begin
>    Key:=#0;
>    End;
> End.

> This routine will allow the numbers 1 thru 8 and the BackSpace key,
> (if
> pressed), to bypass this routine and go to the next routine. As far as
> the numbers and the BackSpace keys are concerned, the routine works
> like
> a charm. The #13 Character is supposed to represent the ENTER key.
> However, on my system, this does not happen. I put a BreakPoint at the
> begining of the routine to trap the users input at moment of
> keystroke.
> All keys except the ENTER, TAB and a few other special keys will trip
> the breakpoint. The Entter and Tab keys will not trip the breakpoint.
> So
> Delphi is not recognizing those keys from my keyboard.

> Don

Other Threads