Delphi: How to make ENTER key works as TAB key in TFrame

delphi

Solution

Here's some example code that would handle a message on the frame to be able to navigate to the next control when Enter is pressed. Note that this sample does not modify the Enter key to become a Tab key. Instead it selects the next control and prevents further processing of the key down message.

Also note that the code may require further tweaking. One for, if any of the controls actually need to process the Enter key, for instance a `TMemo`, you need to add an exception. Second for, the navigation is wrapped in the frame, i.e. after the last frame control the first frame control is focused - not a control on the form and not on the frame. For these, you might want to add conditions for the message return, if you want default processing on some condition simply call inherited without doing any other thing.

type
  TFrame2 = class(TFrame)
    ...
  protected
    procedure CMChildKey(var Message: TCMChildKey); message CM_CHILDKEY;
  end;

..

procedure TFrame2.CMChildKey(var Message: TCMChildKey);
begin
  if Message.CharCode = VK_RETURN then begin
    SelectNext(Screen.ActiveControl, not Bool(GetKeyState(VK_SHIFT) and $80), True);
    Message.Result := 1;
  end else
    inherited;
end;

Problem

I have a Frame and some controls on them (edits, buttons, etc.). How to intercept pressing of ENTER key anywhere on a frame control and translate in to TAB key (taking into account SHIFT status)?

Original source