4
votes

Classic VCL question... but how to do the same within FireMonkey?

I have several standard TControl who can focus...

For instance, in some TEdit, I want to move to the next focusable control if I press the return key

I have try different approach but without success (replacing the key, keychar with 9 when it's 13, ..., forwarding the key events to the form, ...)

Of course, as it is FMX, I'd like a solution which works on all platforms...

If it can avoid to:

  • browse the Children property of the parents,
  • have different pieces of code to address the different platforms
  • hard-code the tab sequence
  • ..., it will be great ;o)
2

2 Answers

10
votes

You can place the following code into the OnKeyDown event of the form:

  if Key = vkReturn then begin
    Key := vkTab;
    KeyDown(Key, KeyChar, Shift);   
  end;

If you want that behaviour only for some of the controls, you must call this code in the OnKeyDown events of those controls.

Be aware that the KeyDown must call the TForm.KeyDown to make it work.

(Tested with XE2)

0
votes

Apart from the suggested answer, to move to next control programmatically one could use SelectNext method provided via TFMXControlClassHelper at:

https://codeverge.com/embarcadero.delphi.firemonkey/fmx-how-to-programmatically-mov/2031600

It uses GetTabList (returns ITabList interface) and FindNextTabStop (returns IControl interface) and SetFocus

According to https://stackoverflow.com/a/36715134/903783 though, "The firemonkey framework prohibits change of focus in some events. In order to change the focus, send a delayed message to the form." (was referring to TabChange Event of TabControl in the handler of which as it seems you can't use SetFocus to focus a child control). So see there for a safer way to do "SetFocus"