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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
procedure TForm1.Edit1KeyDown(Sender
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
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.
Anyway, it works like a charm, and also for other TWinControls than a TForm.
(copied from help:)
procedure TForm1.FormKeyDown(Sender:
begin
if (Key=VK_ESCAPE) and Printer.Printing then
begin
Printer.Abort;
MessageDlg('Printing aborted', mtInformation, [mbOK],0);
end;
end;