如何响应键盘和弦并将输入的字母键替换为特殊键?

How can I respond to a keyboard chord and replace the entered alpha key with a special one?

我想在使用 C# 的 WinForms 应用程序的文本框中,用特殊字符替换某些键盘和弦。例如,如果用户输入"Ctrl+A",我想在文本框中插入字符“á”;如果用户输入 "Ctrl+Shift+A",我想在文本框中插入字符“Á”等

根据我发现的 here,我开始这样做:

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);
} 

...但是它不值得一只没有缝补的袜子在绝望和迫切需要缝补的情况下使用。消息框什么都不显示(一个空字符);如果找到 none,我试图通过返回来抢占单个键,但这也不起作用。

那么我怎样才能有效地响应定义的和弦,并在拦截输入的内容后将一个特殊的键插入文本框?

更新

Idle_Mind的答案很好,但仍有两个键不起作用 - 应该由 Ctrl+Shift+N 产生的“Ñ”和“¡”,因为似乎没有“!”对应的Keys成员我可以尝试改变。

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);
}

评论和注释掉的部分清楚地表明了什么是无效的。

这是为您的表单上的所有文本框完成此操作的一种方法:

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);
    }

}