I can't figure out how to capture the Deletekey press.
I found out that in ASCII code table, it is at 127 place, but if (Key = #127) then
got me nowhere.
Then I checked the value of VK_DELETE
which was 47. Tried to use that, but it didn't work.
The KeyPreview := true
is set in my form.
I tried to add the ShowMessage(IntToStr(Ord(Key)))
to the Forms KeyPress event, but I never got the message popup while clicking the Delete key.
I need to handle the Delete key press in dynamicaly created Edit fields. I want to control what part of the text user can erase in that field, and I know how to handle the text deletion using Backspace key, now need to figure out how to do it with Delete key.
Thanks
Keycode 13 is the Enter key.
KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key.
The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .
You should handle the OnKeyDown
instead of the OnKeyPress
event. If you do that then VK_DELETE
should work for you. Note that the parameter for OnKeyDown
and OnKeyUp
is a Word
, not a Char
as for OnKeyPress
.
Mghie has the correct answer, here is a sample:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=VK_DELETE then
showmessage('Delete key was pressed');
end;
Note that the user can also delete text using cut-to-clipboard so you may need to handle that as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With