Link to home
Start Free TrialLog in
Avatar of WiseGuy
WiseGuy

asked on

I want to conditionally ignore ENTER key stroke.

I knew how to do it. And it has been asked on experts-exchange many times, but I am not going to pay.
Avatar of Limbeck
Limbeck

Use the onkeydown event, figure it out from there

(copied from help:)
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key=VK_ESCAPE) and Printer.Printing then
  begin
  Printer.Abort;
  MessageDlg('Printing aborted', mtInformation, [mbOK],0);
  end;

end;
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  protected
    procedure WMChar(var msg: TWMChar); message WM_CHAR;  
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.WMChar(var msg: TWMChar);
begin
  inherited;
  if msg.CharCode =  13 then
   {Do conditional cheking here}
end;

end.
ASKER CERTIFIED SOLUTION
Avatar of Bijith
Bijith

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pierre Cornelius
Any descendant from the TWinControl should have a OnKeyDown Event. You can use this as follows:

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
//In pseudo-code:

  if Key = VK_RETURN  then
  begin
    if YourConditionsAreMet then
    begin
      DoSomething;
      Key:= 0; //Setting Key to 0 ensures no further processing occurs
    end;
    //if your conditions aren't met, Key would remain as 13 (VK_RETURN)
    //and the control would respond normally.
end;

Regards
Pierre
Avatar of WiseGuy

ASKER

Only Bijith second comment will work. I tried Pierre's before coming here, so ..

Anyway, it works like a charm, and also for other TWinControls than a TForm.