Search code examples
delphidelphi-2009keypress

Capturing a 'delete' key press


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


Solution

  • 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.