Format text in a TRichEdit by means of Shortcuts

Author: Thomas Stutz 

 

 

// Shortcuts to format the selected Text:

// Tastaturkürzel, um den markierten Text zu formatieren:

 

Ctrl + B: Bold         Fett

Ctrl + I: Italic       Kursiv

Ctrl + S: Strikeout    Durchgestrichen

Ctrl + U: fsUnderline  Unterstrichen

 

// Put this Code into your Richedit's OnKeyPress handler:

// Diesen Code ins OnKeyPress Ereignis Prozedur hinzufügen:

 

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);

const

  KEY_CTRL_B = 02;

  KEY_CTRL_I = 9;

  KEY_CTRL_S = 19;

  KEY_CTRL_U = 21;

begin

  with (Sender as TRichEdit).SelAttributes do

    case Ord(Key) of

      KEY_CTRL_B: 

        begin

          Key := #0;

          if fsBold in Style then

            Style := Style - [fsBold]

          else

            Style := Style + [fsBold];

        end;

      KEY_CTRL_I: 

        begin

          Key := #0;

          if fsItalic in Style then

            Style := Style - [fsItalic]

          else

            Style := Style + [fsItalic];

        end;

      KEY_CTRL_S: 

        begin

          Key := #0;

          if fsStrikeout in Style then

            Style := Style - [fsStrikeout]

          else

            Style := Style + [fsStrikeout];

        end;

      KEY_CTRL_U: 

        begin

          Key := #0;

          if fsUnderline in Style then

            Style := Style - [fsUnderline]

          else

            Style := Style + [fsUnderline];

        end;

    end;

end;