How to convert the numeric keypad dot-key into the DecimalSeparator?

384 Views Asked by At

In some applications, like Microsoft Excel, the dot-key from the numeric keypad (VK_DECIMAL) is automatically converted into the current DecimalSeparator.

Picture of a numeric keypad with an arrow pointing to the dot key

I'm trying to implement the same feature but I didn't find a way to make it work in the whole application.

1

There are 1 best solutions below

8
Fabrizio On

At the form level, it can be done by using the form's KeyPreview property and OnKeyPress event handler, for example:

function IsKeyPressed(const AKey : Word) : Boolean;
begin
  Result := GetKeyState(AKey) < 0;
end;

procedure TMyBaseForm.FormCreate(Sender: TObject);
begin
  inherited;
  KeyPreview := True;
end;

procedure TMyBaseForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if(IsKeyPressed(VK_DECIMAL))
  then Key := FormatSettings.DecimalSeparator;
end;

But this solution requires to have a common base form class for all application's forms and won't work with any form/dialog who is not inheriting from that base class (i.e: It will not work with a simple InputQuery either)