Sign in Join
Wiki copytemporary

key down: what changed

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

Title: key down

OldNewLine
1+## Overview
2+
3+The [OnKeyDown](https://lazarus-ccr.sourceforge.io/docs/lcl/controls/twincontrol.onkeydown.html) event of an object allows you to check what key the user has pressed.
4+
5+Note that the procedure keeps track of shift/alt/ctrl etc keys separately (in *Shift*) from the "regular" keys (in *Key*) - see the procedure signature in the example.
6+
7+> **Note:** *OnKeyDown* 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+> **Note:** When a key is held down, the *OnKeyDown* event is re-triggered. The first re-triggering event is after approx 500 ms and the next ones cycle between 30 and 50 ms.
10+
11+## Example
12+
13+``` pascal
14+uses
15+ ...LCLType, Dialogs, ...;
16+ ...
17+procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
18+ Shift: TShiftState);
19+begin
20+ // Example: checking for simple keys:
21+ if (Key = VK_DOWN) or
22+ (Key = VK_UP) then
23+ ShowMessage('Pressed arrow up or down key');
24+ // Check for Alt-F2
25+ if (Key = VK_F2) and (ssAlt in Shift) then
26+ ShowMessage('Alt F2 was pressed')
27+ Key := 0; // Necessary for some widgetsets, e.g. Cocoa, in order to disable processing in subsequent elements.
28+end;
29+```
30+
31+## See also
32+
33+- [Description of keyboard events in Delphi](http://delphi.about.com/od/objectpascalide/a/keyboard_events.htm); should be applicable to Lazarus, as well.
34+- [[LCL Key Handling]] Detailed background on key handling in the LCL.
35+- [[OnKeyPress]]
36+
37+---
38+*From the Free Pascal wiki, page "key down", as it stood on 2020-02-18 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