Sign in Join
Wiki copytemporary

OnKeyPress: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: OnKeyPress

OldNewLine
1+## Overview
2+
3+The [OnKeyPress](https://lazarus-ccr.sourceforge.io/docs/lcl/controls/twincontrol.onkeypress.html) event of an object allows you to check what key the user has pressed.
4+
5+Note that this procedure handles printable characters only. Non-printable characters (e.g. control sequences) are handled by the [[key down|OnKeyDown]] event.
6+
7+> **Note:** *OnKeyPress* doesn't support Unicode characters. If you need Unicode characters but no control characters, use [OnUTF8KeyPress](https://lazarus-ccr.sourceforge.io/docs/lcl/controls/twincontrol.onutf8keypress.html).
8+
9+## Example
10+
11+``` pascal
12+uses
13+ ...LCLType, Dialogs, ...;
14+ ...
15+
16+procedure TMainForm.FormKeyPress(Sender: TObject; var Key: char);
17+begin
18+ case key of
19+ '0': ShowMessage('"0" key pressed');
20+ '1': ShowMessage('"1" key pressed');
21+ '2': ShowMessage('"2" key pressed');
22+ end;
23+ Key := #0; // Necessary for some widgetsets, e.g. Cocoa, in order to disable processing in subsequent elements.
24+end;
25+```
26+
27+## See also
28+
29+- [[LCL Key Handling]] Detailed background on key handling in the LCL.
30+- [[key down]]
31+
32+---
33+*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](/wiki-contact).*

Back to history