Delphi VCL app, how to disable the default behavior of a KeyDown event?

Solution 1:

The key here (no pun intended) is to use OnKeyPress instead of OnKeyDown:

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = 'x' then
  begin
    Key := #0;
    Memo1.Lines.Add('yes');
  end;
end;

Solution 2:

Not an answer, but a comment, but I need to publish code, so...

You can test the current Shift-state using these functions:

CONST
  VK_ALT        = VK_MENU;
  VK_CTRL       = VK_CONTROL;

FUNCTION KeyPressed(VirtualKey : WORD) : BOOLEAN;
  BEGIN
    Result:=(GetKeyState(VirtualKey) AND $80000000<>0)
  END;

FUNCTION Shift : BOOLEAN;
  BEGIN
    Result:=KeyPressed(VK_SHIFT)
  END;

FUNCTION Alt : BOOLEAN;
  BEGIN
    Result:=KeyPressed(VK_ALT)
  END;

FUNCTION Ctrl : BOOLEAN;
  BEGIN
    Result:=KeyPressed(VK_CTRL)
  END;

But beware that you must call these function at the tail end of a keyboard event and before a new keyboard event occurs, as this call will return the state as it was from the latest keyboard event and not the one that necessarily were active at the time you want (if you call it outside the keyboard event handler).