Skip to content Skip to sidebar Skip to footer

How To Detect Virtual Keyboard Events In Android With Delphi

I'm trying to develop an Android app with Rad studio Xe5 with Delphi, and i having the following problem: There is a Tmemo, which is at the bottom of the screen, and at the time of

Solution 1:

I had the same problem with the keyboard over tmemo, try these two functions and the events OnVirtualKeyboardShown and OnVirtualKeyboardHidden

public
    { Public declarations }
    FSavedY: Single;
    FocusControl: TControl;
    ParentedControl: TFMXObject;
    function FocusedControl: TControl;
    function GetFocusedControlOffset(KeyboardRect: TRect): Single;

...

function TfrmFeedBackMobile.FocusedControl: TControl;
begin
  Result := nil;
  if Assigned(Focused) and (Focused.GetObject is TControl) then
    Result := TControl(Focused.GetObject);
end;

function TfrmFeedBackMobile.GetFocusedControlOffset(KeyboardRect: TRect): Single;
var
  Control: TControl;
  ControlPos: TPointF;
  KeyboardTop: Single;
begin
  Result := 0;
  KeyboardTop := Height - (KeyboardRect.Bottom - KeyboardRect.Top) - 66;
  // At least, should be. 66 is the height of the keyboard "done" bar
  Control := FocusedControl;
  if Assigned(Control) then
  begin
    ControlPos := Control.LocalToAbsolute(PointF(0, 0));
    Result := KeyboardTop - ControlPos.Y + Control.Height + 2;
    if Result >= 0 then
      Result := 0;
  end;
end;

procedure TfrmFeedbackMobile.FormVirtualKeyboardHidden(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  FocusControl.Parent:= ParentedControl;
  FocusControl.AnimateFloat('Position.Y', FSavedY, 0.1);
  FocusControl.Align := TAlignLayout.alClient;
  FocusControl:= nil;
end;

procedure TfrmFeedbackMobile.FormVirtualKeyboardShown(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  FocusControl:= FocusedControl;
  if not (FocusControl is TMemo) then Exit;

  FocusControl.Align := TAlignLayout.alNone;
  FSavedY := FocusControl.Position.Y;
  FocusControl.Position.Y:= 0;
  FocusControl.AnimateFloat('Position.Y',
    FSavedY + GetFocusedControlOffset(Bounds), 0.1);
  ParentedControl:= FocusControl.Parent;
  FocusControl.Parent:= frmFeedbackMobile;
  FocusControl.BringToFront;
end;

Post a Comment for "How To Detect Virtual Keyboard Events In Android With Delphi"