Capture the keystroke delete

I cannot figure out how to record the keystroke Delete . I found out that in the ASCII code table it is at 127th place, but if (Key = #127) then didn’t deliver me anywhere.

Then I checked the value of VK_DELETE , which was 47. I tried to use it, but it did not work.

KeyPreview := true set in my form.

I tried to add ShowMessage(IntToStr(Ord(Key))) to the Forms KeyPress event, but never received a pop-up message when I pressed the Delete key.

I need to handle Delete key presses in dynamically created edit fields. I want to control how much of a text user can be erased in this field, and I know how to handle deleting text using the Backspace key, now I need to figure out how to do this using the Delete key.

thanks

+4
source share
3 answers

You should handle OnKeyDown instead of the OnKeyPress event. If you do this, then VK_DELETE should work for you. Please note that the parameter for OnKeyDown and OnKeyUp is Word , not Char as for OnKeyPress .

+10
source

Mghie has the correct answer, here is an example:

 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 also need to process it.

+10
source

You can use OnKeyDown to filter out the unwanted Delete key:

 procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_DELETE then begin Beep; Key:= 0; end; end; 
+4
source

Source: https://habr.com/ru/post/1303326/


All Articles