Difference between revisions of "key down"

From Free Pascal wiki
Jump to navigationJump to search
m
(→‎Example: Use unit LCLType instead of Windows)
Line 8: Line 8:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
uses
 
uses
   ...Windows, Dialogs, ...;
+
   ...LCLType, Dialogs, ...;
 
   ...   
 
   ...   
 
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
 
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;

Revision as of 22:26, 12 January 2015

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.

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')
end;

See also