Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

OnKeyPress

Overview

The OnKeyPress event of an object allows you to check what key the user has pressed.

Note that this procedure handles printable characters only. Non-printable characters (e.g. control sequences) are handled by the OnKeyDown event.

Note: OnKeyPress doesn't support Unicode characters. If you need Unicode characters but no control characters, use OnUTF8KeyPress.

Example

pascal
uses
  ...LCLType, Dialogs, ...;
  ...  

procedure TMainForm.FormKeyPress(Sender: TObject; var Key: char);
begin
  case key of
    '0': ShowMessage('"0" key pressed');
    '1': ShowMessage('"1" key pressed');
    '2': ShowMessage('"2" key pressed');
  end;
  Key := #0; // Necessary for some widgetsets, e.g. Cocoa, in order to disable processing in subsequent elements.
end;

See also


From the Free Pascal wiki, page "OnKeyPress", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .