Jump to content
Sign in to follow this  
msd

Using VK_XXX numeric part of keyboard

Recommended Posts

Hello,

 

On the form OnFormKey UP or DWN, we can use the entire keyboard, but for the numeric part, we have VK_ADD, VK_MULTIPLY,...

I need some advice for using those keys in OnFormKeyDown, Up, or Press like this sample below:

When a user searches for data in the TEdit VCL by typing letters and numbers and then clicking on the numeric + key VK_ADD, the app does something BUT WITHOUT adding +char to the TEdit VCL control.

Simple: I need to use those keys but without the characters they represent: +, -, /, and *.

 

Thanks for the advice in advance...

Share this post


Link to post

SWAG would be to capture the key in OnKeyDown test for the key if a VK_ADD, set the KeyDown(var Key: Word; to 0 and add the + to the edit field.

Same for the other -/etc. keys.

 

And you might need to turn on KeyPreview for the form.

 

  • Thanks 1

Share this post


Link to post
14 hours ago, msd said:

Hello,

 

On the form OnFormKey UP or DWN, we can use the entire keyboard, but for the numeric part, we have VK_ADD, VK_MULTIPLY,...

I need some advice for using those keys in OnFormKeyDown, Up, or Press like this sample below:

When a user searches for data in the TEdit VCL by typing letters and numbers and then clicking on the numeric + key VK_ADD, the app does something BUT WITHOUT adding +char to the TEdit VCL control.

Simple: I need to use those keys but without the characters they represent: +, -, /, and *.

 

Thanks for the advice in advance...

Use the OnKeyPress event of the edit control. Like this:

 

procedure TForm2.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = '+') and (GetKeyState(VK_ADD) < 0)
  then begin
    Key := #0;
    label1.Caption := 'VK_ADD';
  end
  else
    label1.Caption := string.Empty;
end;

The GetKeyState API function allows you to check which key is currently down. The code above will let the '+' key of the normal keyboard add the character to the edit but block the numpad plus key input.

Edited by PeterBelow
  • Thanks 1

Share this post


Link to post
{$R *.dfm}

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key in [VK_ADD, VK_MULTIPLY { ,...] } ]) then
    begin
      Key := 0;
      Memo1.Lines.Add('OnKeyDown: VK_ADD, VK_MULTIPLY, ...  pressed');
    end;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if CharInSet(Key, ['+', '-', '/', '*']) then
    begin
      Key := #0;
      Memo1.Lines.Add('OnKeyPress: VK_ADD, VK_.... pressed');
    end;
end;

end.

image.png.7b619128a4dd2ad236bf3461367d9f03.png

Edited by programmerdelphi2k
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×