key down
From Lazarus wiki
Jump to navigationJump to search
│
Deutsch (de) │
English (en) │
Overview
The OnKeyDown event of an object allows you to check what key the user has pressed.
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.
![Note icon Light bulb](https://upload.wikimedia.org/wikipedia/commons/d/d8/Nuvola_apps_ktip.png)
![Note icon Light bulb](https://upload.wikimedia.org/wikipedia/commons/d/d8/Nuvola_apps_ktip.png)
Example
uses
...LCLType, Dialogs, ...;
...
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
// Example: checking for simple keys:
if (Key = VK_DOWN) or
(Key = VK_UP) then
ShowMessage('Pressed arrow up or down key');
// Check for Alt-F2
if (Key = VK_F2) and (ssAlt in Shift) then
ShowMessage('Alt F2 was pressed')
Key := 0; // Necessary for some widgetsets, e.g. Cocoa, in order to disable processing in subsequent elements.
end;
See also
- Description of keyboard events in Delphi; should be applicable to Lazarus, as well.
- LCL Key Handling Detailed background on key handling in the LCL.
- OnKeyPress