Skip to content

Commit 880406a

Browse files
authored
Merge pull request #29 from Embarcadero/keyevent
Including the TKeyevent event handler - Fixes Embarcadero/DelphiFMX4Python#25
2 parents 991fcf2 + 5dcc559 commit 880406a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Source/fmx/WrapDelphiFmx.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ implementation
1212
{$ENDIF MSWINDOWS}
1313
WrapDelphiDataBind,
1414
WrapFmxTypes,
15+
WrapFmxControls,
1516
WrapFmxStdCtrls,
1617
WrapFmxEdit,
1718
WrapFmxListBox,

Source/fmx/WrapFmxControls.pas

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ interface
1616
SetBounds and Repaint
1717
Exposes properties Parent and Visible
1818
}
19+
20+
{ TKeyEvent wrapper }
21+
TKeyEventHandler = class(TEventHandler)
22+
protected
23+
procedure DoEvent(Sender: TObject; var Key: Word; var KeyChar: WideChar; Shift: TShiftState);
24+
public
25+
constructor Create(APyDelphiWrapper: TPyDelphiWrapper; AComponent: TObject;
26+
APropertyInfo: PPropInfo; ACallable: PPyObject); override;
27+
class function GetTypeInfo: PTypeInfo; override;
28+
end;
29+
1930
TPyDelphiControl = class (TPyDelphiFmxObject)
2031
private
2132
function GetDelphiObject: TControl;
@@ -179,6 +190,8 @@ procedure TControlsRegistration.RegisterWrappers(
179190
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiStyleBook);
180191
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiPopup);
181192
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiCustomControlAction);
193+
194+
APyDelphiWrapper.EventHandlers.RegisterHandler(TKeyEventHandler);
182195
end;
183196

184197
{ TPyDelphiControl }
@@ -769,6 +782,75 @@ procedure TPyDelphiCustomControlAction.SetDelphiObject(
769782
inherited DelphiObject := Value;
770783
end;
771784

785+
{ TKeyEventHandler }
786+
787+
constructor TKeyEventHandler.Create(APyDelphiWrapper: TPyDelphiWrapper;
788+
AComponent: TObject; APropertyInfo: PPropInfo; ACallable: PPyObject);
789+
var
790+
LMethod : TMethod;
791+
begin
792+
inherited;
793+
LMethod.Code := @TKeyEventHandler.DoEvent;
794+
LMethod.Data := Self;
795+
SetMethodProp(AComponent, APropertyInfo, LMethod);
796+
end;
797+
798+
class function TKeyEventHandler.GetTypeInfo: PTypeInfo;
799+
begin
800+
Result := System.TypeInfo(TKeyEvent);
801+
end;
802+
803+
procedure TKeyEventHandler.DoEvent(Sender: TObject; var Key: Word;
804+
var KeyChar: WideChar; Shift: TShiftState);
805+
var
806+
LPyObject: PPyObject;
807+
LPyTuple: PPyObject;
808+
LPyResult: PPyObject;
809+
LPyKey: PPyObject;
810+
LVarKeyParam: TPyDelphiVarParameter;
811+
LPyKeyChar: PPyObject;
812+
LVarKeyCharParam: TPyDelphiVarParameter;
813+
LKeyCharStr: string;
814+
begin
815+
Assert(Assigned(PyDelphiWrapper));
816+
if Assigned(Callable) and PythonOK then
817+
with GetPythonEngine do begin
818+
LPyObject := PyDelphiWrapper.Wrap(Sender);
819+
//var parameters
820+
LPyKey := CreateVarParam(PyDelphiWrapper, Key);
821+
LVarKeyParam := PythonToDelphi(LPyKey) as TPyDelphiVarParameter;
822+
LPyKeyChar := CreateVarParam(PyDelphiWrapper, KeyChar);
823+
LVarKeyCharParam := PythonToDelphi(LPyKeyChar) as TPyDelphiVarParameter;
824+
825+
LPyTuple := PyTuple_New(4);
826+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 0, LPyObject);
827+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 1, LPyKey);
828+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 2, LPyKeyChar);
829+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 3, ShiftToPython(Shift));
830+
try
831+
LPyResult := PyObject_CallObject(Callable, LPyTuple);
832+
if Assigned(LPyResult) then
833+
begin
834+
Py_DECREF(LPyResult);
835+
if PyLong_Check(LVarKeyParam.Value) then
836+
Key := PyLong_AsLong(LVarKeyParam.Value);
837+
838+
if (LVarKeyCharParam.Value = Py_None) then
839+
KeyChar := #0
840+
else if PyUnicode_Check(LVarKeyCharParam.Value) then
841+
begin
842+
LKeyCharStr := PyUnicodeAsString(LVarKeyCharParam.Value);
843+
if Length(LKeyCharStr) > 0 then
844+
KeyChar := LKeyCharStr[1];
845+
end;
846+
end;
847+
finally
848+
Py_DECREF(LPyTuple);
849+
end;
850+
CheckError();
851+
end;
852+
end;
853+
772854
initialization
773855
RegisteredUnits.Add(TControlsRegistration.Create);
774856

0 commit comments

Comments
 (0)