Board index » delphi » Normal Enter or Num Enter?

Normal Enter or Num Enter?

Hi all,

Can I know if the user has pressed the normal ENTER key or the one located
at the num-pad?

TIA

 

Re:Normal Enter or Num Enter?


Quote
> Can I know if the user has pressed the normal ENTER key or the one located
> at the num-pad?

The only difference between these keys is that, in the WM_KEY* and WM_CHAR  
messages created by them bit 24 of the message lparam will be set for the
numpad key and clear for the typewriter keyboard key. You have to attach a
handler to Application.OnMessage to see the message lparam and save it for
later inspection in an OnKey event handler. Here is an example to play
around with:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DBCtrls, DBCGrids, Db, DBTables, ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FLastMsg: TMsg;

    procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean);
begin
  Case msg.message of
    WM_KEYFIRST..WM_KEYLAST:
      FLastMsg := Msg;
  End;
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
  Function keyname: String;
  begin
    SetLength( result, 128 );
    SetLength( result,
               GetKeynameText( FLastMsg.lparam,
                               @result[1],
                               Length( result )));
  end;
  Function Scancode: Byte;
  begin
    result := LoByte( HiWord( FLastMsg.lparam ));
  end;
  Function specialkey: String;
  begin
    if (FLastMsg.lparam and $1000000) <> 0 Then
      result := 'yes'
    else
      result := 'no';
  end;
begin
  memo1.lines.add( format(
    'key: <%s>; scancode: %d; specialkey: %s',
    [ Keyname, scancode, specialkey ] ));
  key := #0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := AppOnMessage;
end;

end.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Other Threads