Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 446
user1651105 Avatar asked Mar 06 '10 10:03

user1651105


People also ask

What is e keycode === 13?

Keycode 13 is the Enter key.

What is the difference between Keyup and Keydown?

KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key.

What is the difference between keypress and Keydown?

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 .


2 Answers

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.

like image 114
mghie Avatar answered Nov 02 '22 11:11

mghie


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.

like image 14
Ville Krumlinde Avatar answered Nov 02 '22 12:11

Ville Krumlinde