Jump to content
PeterPanettone

Strange effect in TRichEdit: CTRL+I outputs TAB

Recommended Posts

At run-time, select any text in a TRichEdit and press CTRL+IThe selected text will be replaced by a TAB "character," which has the same effect as pressing the TAB key.

 

BTW, the same effect occurs in Notepad.

 

Is there a way to prevent this?

 

Delphi 12.2

Windows 11 x64

Edited by PeterPanettone

Share this post


Link to post

I don't think you can do something, except to capture the keyboard in the control (or in the OnKey... of the Form with the KeyPreview property active).

 

In the event intercept the combination key and put tha value to ZERO (0).

 

Take care that you must try wich event you can use ... not all the OnKey... events are the same scope.

 

Edit: you can see an example how to implement the OnKeyDown event in a TControl descendent here: https://docwiki.embarcadero.com/RADStudio/Athens/en/Responding_to_Key-down_Messages

 

Bye

 

Edited by DelphiUdIT

Share this post


Link to post

Even when KeyPreview = True, setting Key := 0; in FormKeyDown does not prevent this strange effect:

 

procedure TformTextEditor.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = Ord('I')) and (Shift = [ssCtrl]) then
  begin
    CodeSite.Send('TformTextEditor.FormKeyDown: ');
    Key := 0;
  end
end;

 

Share this post


Link to post

Using KeyPreview, I have even tried the following trick:

procedure TformTextEditor.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = Ord('I')) and (Shift = [ssCtrl]) then
  begin
    CodeSite.Send('TformTextEditor.FormKeyDown: ');
    Key := 255;
  end
end;

And then in RichEditKeyDown:

  if (Key = 255) and (Shift = [ssCtrl]) then
  begin
    CodeSite.Send('RichEditKeyDown: ');    
    Key := 0;
    // does not prevent TAB output!
  end

 

Edited by PeterPanettone

Share this post


Link to post
1 hour ago, PeterPanettone said:

Even when KeyPreview = True, setting Key := 0; in FormKeyDown does not prevent this strange effect:

 


procedure TformTextEditor.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = Ord('I')) and (Shift = [ssCtrl]) then
  begin
    CodeSite.Send('TformTextEditor.FormKeyDown: ');
    Key := 0;
  end
end;

 

You have to use OnKeyPress and check for Ctrl down there. The behaviour you see is perfectly OK, by the way. Ctrl-<letter> combos have created control characters since the ancient days of DOS, and ^I (#9) is the TAB character...

  • Like 1

Share this post


Link to post
3 minutes ago, PeterBelow said:

Ctrl-<letter> combos have created control characters since the ancient days of DOS, and ^I (#9) is the TAB character...

Thanks for the information.

Share this post


Link to post

Another solution could be to subclass TRichEdit and then (temporarily) suppress the TAB character, but it's not worth the effort.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×