0

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 Answer 1

-1

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)

Sign up to request clarification or add additional context in comments.

8 Comments

To make it work in all forms, drop a TApplicationEvents on your main form and add an OnMessage handler. Look for WM_CHAR and then replace the Msg.wParam accordingly.
@AndreasRejbrand: How can I distinguish between VK_DECIMAL and VK_OEM_PERIOD? The wParam is 46 in both cases
The ugly solution is to continue to use your IsKeyPressed(VK_DECIMAL) approach. I haven't had time to investigate the alternatives.
Remember to also check for the NumLock state. Otherwise someone might expect "Del" key action when pressing that button, and be confused that a decimal point arrives instead.
OnKeyPress/WM_CHAR deal in translated characters, not in virtual keys. To process virtual keys, you need to handle OnKey(Down|Up)/WM_KEY(DOWN|UP) instead
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.