wißt ihr wie man eine LCL-Komponente erstellt, welche auf Tastatureingaben (focused) reagiert. Ich habe eins geschrieben und das mit einem TCheckBox auf ein Form gepackt. Leider bleibt nur TCheckbox "focused", meine Kompenente bekommt keine Tastatur-Ereignisse auch mit Tabulator-Taste kann ich nicht meine Komponente erreichen.
Hier ist der Quelltext. Was muß ich da machen damit die Komponente wie ein TEdit/TCheckBox Tastatur-Ereignisse empfangen kann?
Code: Alles auswählen
unit testctrl;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, controls;
type
TTestBox = class(TCustomControl)
private
protected
procedure Paint; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override; //hinzugefügt
public
constructor Create(AOwner: TComponent); override;
//destructor Destroy; override;
function CanFocus: Boolean; override;
published
property Caption;
end;
implementation
constructor TTestBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//ControlStyle:=ControlStyle-[csNoFocus];
//ControlStyle:=ControlStyle+[csClickEvents, csCaptureMouse];
TabStop := True;
end;
function TTestBox.CanFocus: Boolean;
begin
Result:= true;
end;
procedure TTestBox.Paint;
begin
Canvas.TextOut(1,1,Caption);
end;
procedure TTestBox.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
Caption:='keydown: '+IntToStr(Key); //geändert
Paint;
end;
procedure TTestBox.MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); //hinzugefügt
begin
inherited ;
SetFocus;
end;
end.
Falls irgendjemand das liest und vielleicht das als Grundlage für sein Komponente verwenden will. Ich habe Code im Posting geändert (siehe //geändert //hinzugefügt) jetzt funktioniert das auch mit Mausklick.