In other words, if you want to get the character that will appear in the
TMemo, use
OnKeyPress. It is usually used to filter input, for example, to only allow numbers when entering a phone number (example is not complete):
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: char);
begin
if not (Key in ['0'..'9']) then
Key := #0;
end;
By the way, it is better to use the
UTF8KeyPress event, which will allow you to get characters of national alphabets.
If you need to know the key itself (regardless of the current keyboard layout), use
OnKeyDown. It is usually used to handle key shortcuts:
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = VK_RETURN) and (Shift = [ssCtrl]) then // Ctrl+Enter
begin
ShowMessage('test');
Key := 0;
end;
end;
Key constants (
VK_) can be found in the
LCLType unit. Pay attention to the comments to them, some keys may depend on the standard keyboards in each country.
Assigning zero here is necessary so that this key is not passed on further. For example, you can reassign the combination in
TMemo itself.