-3

I want to, in a textbox on a WinForms app using C#, replace certain keyboard chords with special characters. For example, if the user enters "Ctrl+A", I want to insert into the textbox the character "á"; if the user enters "Ctrl+Shift+A", I want to insert into the textbox the character "Á", etc.

Based on what I found here, I started off with this:

private void textBox_KeyDown(object sender, KeyEventArgs keArgs)
{
    bool useHTMLCodes = checkBoxUseHTMLCodes.Checked;
    String replacement = null;
    if (Control.ModifierKeys == Keys.None) return; // doesn't work
    if (useHTMLCodes)
    {
        if (Control.ModifierKeys == Keys.Control && keArgs.KeyCode == Keys.A)
        {
            replacement = "á";
        }
        else if (Control.ModifierKeys == Keys.Control && Control.ModifierKeys == Keys.Shift && keArgs.KeyCode == Keys.A)
        {
            replacement = "Á";
        }
    }
    else // just replace with the raw char, not the fancy-pants HTML code
    {
        if (Control.ModifierKeys == Keys.Control && keArgs.KeyCode == Keys.A)
        {
            replacement = "á";
        }
        else if (Control.ModifierKeys == Keys.Control && Control.ModifierKeys == Keys.Shift && keArgs.KeyCode == Keys.A)
        {
            replacement = "Á";
        }
    }
    MessageBox.Show(replacementChar);
} 

...but it doesn't work worth an undarned sock in desperate and dire need of darning. The messagebox shows nothing (an empty char); I tried to preempt individual keys by returning if none were found, but that doesn't work, either.

So how can I, in effect, respond to defined chords, and insert a special key into the textbox after intercepting what was keyed in?

UPDATE

Idle_Mind's answer was great, but there are still two keys that are not working - the "Ñ", which should be produced by Ctrl+Shift+N, and the "¡", because there seems to be no Keys member corresponding to "!" that I can try to shift.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (this.ActiveControl != null && this.ActiveControl is TextBox)
    {
        string replacement = "";
        TextBox tb = (TextBox)this.ActiveControl;
        bool useHTMLCodes = checkBoxUseHTMLCodes.Checked;

        // A
        if (keyData == (Keys.Control | Keys.A))
        {
            replacement = useHTMLCodes ? "á" : "á";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.A))
        {
            replacement = useHTMLCodes ? "Á" : "Á";
        }
        // E
        if (keyData == (Keys.Control | Keys.E))
        {
            replacement = useHTMLCodes ? "é" : "é";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.E))
        {
            replacement = useHTMLCodes ? "É" : "É";
        }
        // I
        if (keyData == (Keys.Control | Keys.I))
        {
            replacement = useHTMLCodes ? "í" : "í";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.I))
        {
            replacement = useHTMLCodes ? "Í" : "Í";
        }
        // O
        if (keyData == (Keys.Control | Keys.O))
        {
            replacement = useHTMLCodes ? "ó" : "ó";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.O))
        {
            replacement = useHTMLCodes ? "Ó" : "Ó";
        }
        // U
        if (keyData == (Keys.Control | Keys.U))
        {
            replacement = useHTMLCodes ? "ú" : "ú";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.U))
        {
            replacement = useHTMLCodes ? "Ú" : "Ú";
        }
        // U Umlauts
        if (keyData == (Keys.Control | Keys.Alt | Keys.U))
        {
            replacement = useHTMLCodes ? "ü" : "ü";
        }
        else if (keyData == (Keys.Control | Keys.Alt | Keys.Shift | Keys.U))
        {
            replacement = useHTMLCodes ? "Ü" : "Ü";
        }
        // N
        if (keyData == (Keys.Control | Keys.N))
        {
            replacement = useHTMLCodes ? "ñ" : "ñ";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.N))
        {
            replacement = useHTMLCodes ? "Ñ" : "Ñ"; // not working
        }
        // ?
        if (keyData == (Keys.Control | Keys.OemQuestion))
        {
            replacement = useHTMLCodes ? "¿" : "¿";
        }
        // !
        //if (keyData == (Keys.Control | Keys.)) // what is the exclamation point?
        //{
        //    replacement = useHTMLCodes ? "¡" : "¡";
        //}

        if (replacement != "")
        {
            tb.SelectedText = replacement;
            return true;
        }
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

The comment and the commented-out portion make clear what is not working.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    Dear Shannon, after asking [more than 1600 questions](http://stackoverflow.com/users/875317/b-clay-shannon?tab=questions) you should by now know that instead of some nice piece of prose we'd like to [actually see what you expect to see, what you did actually see and what you have tried to explain those differences](http://stackoverflow.com/help/how-to-ask). There's lots of text here, but it comes down to _"This doesn't work, debug this for me"_, to which I reply _"No"_. What are for example the values of the various properties of `KeyEventArgs` when you press the keys you wish to intercept? – CodeCaster Jul 01 '15 at 18:50
  • Idle_Mind figured it out, Caster. – B. Clay Shannon-B. Crow Raven Jul 01 '15 at 22:02
  • 1
    You could figure the rest out very easily. Add a `Debug.WriteLine(keyData)` and you'll see the mask to use for the keys you press. You _really_ need to put more effort into debugging your own code. – CodeCaster Jul 02 '15 at 09:47
  • Who woulda thunk the exclamation point/mark/char is "D1"...goofy. – B. Clay Shannon-B. Crow Raven Jul 02 '15 at 14:57
  • Added a followup question at http://stackoverflow.com/questions/31188543/how-can-i-capture-ctrlshiftn – B. Clay Shannon-B. Crow Raven Jul 02 '15 at 15:19

1 Answers1

2

Here's one way to accomplish this for all TextBoxes on your Form:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (this.ActiveControl != null && this.ActiveControl is TextBox)
        {
            string replacement = "";
            TextBox tb = (TextBox)this.ActiveControl;
            bool useHTMLCodes = checkBoxUseHTMLCodes.Checked;

            if (keyData == (Keys.Control | Keys.A))
            {
                replacement = useHTMLCodes ? "á" : "á";
            }
            else if (keyData == (Keys.Control | Keys.Shift | Keys.A))
            {
                replacement = useHTMLCodes ? "Á" : "Á";
            }

            if (replacement != "")
            {
                tb.SelectedText = replacement;
                return true;
            }
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40