We help IT Professionals succeed at work.

I want to conditionally ignore ENTER key stroke.

WiseGuy
WiseGuy asked
on
299 Views
Last Modified: 2010-04-05
I knew how to do it. And it has been asked on experts-exchange many times, but I am not going to pay.
Comment
Watch Question

Commented:
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;

Commented:
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.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.