I am currently tasked with programming a text editor for some formatted text. I chose to use a RichTextBox for obvious reasons. For every key until now I was able to modify the KeyChar of it, either with overriding the ProcessCmdKey method or with subscribing to the KeyPress event or overriding the virtual method OnKeyPress.
The rtf text I use in this example is this:
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Courier New;}{\f1\fnil Courier New;}{\f2\fnil Verdana;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 10.0.19041}\viewkind4\uc1 \pard\sa180\sl72\slmult1\ulc1\ul\b\f0\fs21 Header1:\ulc1\ulnone\b0\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\ul\b\f0 Header2:\ulc1\ulnone\b0\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\ul\b\f0 Header3:\ulc1\ulnone\b0\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\ulc1\f0 Lorem ipsum.\ulc1\f1\line\line\ulc1\f0 Signature...\ulc1\f2\fs20\par }
So with this rtf basically I am trying to simulate Shift Enter every time a user presses Enter, so that the line break is smaller than the default one when pressing Enter.
I declared my own RichTextBox for this, in hope I could dig in a bit deeper:
public class CustomRichTextBox : RichTextBox { ... }
To clarify what I tried so far - these were my approaches, which worked perfectly for normal chars:
using ProcessCmdKey to change the character A to B:
protected override bool ProcessCmdKey(ref Message m, Keys keyData)
{
if(keyData == Keys.A)
{
m.WParam = new IntPtr((int)Keys.B);
return base.ProcessCmdKey(ref m, Keys.B);
}
else if(keyData == (Keys.Shift | Keys.A))
{
m.WParam = new IntPtr((int)(Keys.Shift | Keys.B));
return base.ProcessCmdKey(ref m, (Keys.Shift | Keys.B));
}
return base.ProcessCmdKey(ref m, keyData);
}
using OnKeyPress to change the character A to B:
protected override void OnKeyPress(KeyPressEventArgs e)
{
if(e.KeyChar == 'A')
{
e.KeyChar = 'B';
}
else if(e.KeyChar == 'a')
{
e.KeyChar = 'b';
}
base.OnKeyPress(e);
}
Only for me pressing Enter, it doesn't work as expected (no effect for me):
protected override bool ProcessCmdKey(ref Message m, Keys keyData)
{
if (keyData == Keys.Enter)
{
m.WParam = new IntPtr((int)(Keys.Shift | Keys.Enter));
return base.ProcessCmdKey(ref m, (Keys.Shift | Keys.Enter));
}
return base.ProcessCmdKey(ref m, keyData);
}
Is there no simple way to realize this or did I miss something?
Edit:
I forgot to include the approach with OnKeyPress. Here is what I tried:
protected override void OnKeyPress(KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.KeyChar = '\u2028';
}
base.OnKeyPress(e);
}
And here the result:
CodePudding user response:
As it turned out, it was embarassingly easy to realize with using SendKeys and blocking the Enter KeyPress by returning true...
private bool sendingEnter = false;
public override bool PreProcessMessage(ref Message msg)
{
if(msg.Msg == WM_KEYDOWN && msg.WParam.ToInt32() == VK_RETURN)
{
if (!sendingEnter)
{
sendingEnter = true;
SendKeys.Send(" {ENTER}");
return true;
}
else
{
sendingEnter = false;
}
}
return base.PreProcessMessage(ref msg);
}

