00 Votes

Delphi: Select all with CTRL+A in Memo

Question by Chematik | 2012-09-10 at 17:42

I have a TMemo on a form. When I press the key combination CTRL and A in the memo, I expect that everything will be marked. But nothing happens with this shortcut.

How can that be? Would Windows not have to ensure that CTRL-A is working? And how can I achieve it, that these keys are working?

ReplyPositiveNegativeDateVotes
6Best Answer6 Votes

A memo field only shows this behavior, if you add the following code in the OnKeyDown event of the memo:

procedure TForm1.Memo1KeyDown(Sender: TObject; 
var Key: Word; Shift: TShiftState);
begin          
  if ((ssCtrl in Shift) and (Key = 65)) then
    TMemo(Sender).SelectAll;  //select content of memo
end;

With this, at every keystroke, it will be looked, whether the user hits the key A while the CTRL key is presed. The 65 is the code of A, which you can also retrieve with ord(A).
2012-09-10 at 22:20

ReplyPositive Negative
11 Vote

Here's yet a better way to handle it...

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^A then begin
    (Sender as TMemo).SelectAll;
    Key := #0;
  end;
end;

2019-03-10 at 16:51

ReplyPositive Negative
00 Votes

Thank you I tryed the second it works.

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^A then begin
    (Sender as TMemo).SelectAll;
    Key := #0;
  end;
end;

2019-11-07 at 08:47

ReplyPositive Negative
Reply

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.