Difference between revisions of "key down"

From Lazarus wiki
Jump to navigationJump to search
(→‎Overview: + Links to doc; Not on Unicode characters)
m (Fixed syntax highlighting; deleted category included in page template)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{key down}}
 
{{key down}}
 +
 
== Overview ==
 
== Overview ==
 +
 
The {{Doc|package=LCL|unit=controls|identifier=twincontrol.onkeydown|text=OnKeyDown}} event of an object allows you to check what key the user has pressed.
 
The {{Doc|package=LCL|unit=controls|identifier=twincontrol.onkeydown|text=OnKeyDown}} event of an object allows you to check what key the user has pressed.
  
Line 6: Line 8:
  
 
{{Note|''OnKeyDown'' doesn't support Unicode characters. If you need Unicode characters but no control characters, use {{Doc|package=LCL|unit=controls|identifier=twincontrol.onutf8keypress|text=OnUTF8KeyPress}}.}}
 
{{Note|''OnKeyDown'' doesn't support Unicode characters. If you need Unicode characters but no control characters, use {{Doc|package=LCL|unit=controls|identifier=twincontrol.onutf8keypress|text=OnUTF8KeyPress}}.}}
 +
 +
{{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.}}
  
 
== Example ==
 
== Example ==
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
uses
 
uses
 
   ...LCLType, Dialogs, ...;
 
   ...LCLType, Dialogs, ...;
Line 22: Line 27:
 
   if (Key = VK_F2) and (ssAlt in Shift) then
 
   if (Key = VK_F2) and (ssAlt in Shift) then
 
     ShowMessage('Alt F2 was pressed')
 
     ShowMessage('Alt F2 was pressed')
 +
  Key := 0; // Necessary for some widgetsets, e.g. Cocoa, in order to disable processing in subsequent elements.
 
end;  
 
end;  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== See also ==
 
== See also ==
 +
 
* [http://delphi.about.com/od/objectpascalide/a/keyboard_events.htm Description of keyboard events in Delphi]; should be applicable to Lazarus, as well.
 
* [http://delphi.about.com/od/objectpascalide/a/keyboard_events.htm Description of keyboard events in Delphi]; should be applicable to Lazarus, as well.
 
* [[LCL Key Handling]] Detailed background on key handling in the LCL.
 
* [[LCL Key Handling]] Detailed background on key handling in the LCL.
 
+
* [[OnKeyPress]]
[[Category:LCL]]
 

Latest revision as of 07:32, 18 February 2020

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.

Light bulb  Note: OnKeyDown doesn't support Unicode characters. If you need Unicode characters but no control characters, use OnUTF8KeyPress.
Light bulb  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.

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