-3

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

根据我在这里找到的内容,我从这个开始:

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

...但它不值得在绝望和迫切需要织补的情况下穿上一件没有织补的袜子。消息框不显示任何内容(一个空字符);如果没有找到,我试图通过返回来抢占单个键,但这也不起作用。

那么实际上,我如何才能响应定义的和弦,并在截取键入的内容后将特殊键插入文本框?

更新

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

注释和注释掉的部分清楚地表明了什么不起作用。

4

1 回答 1

2

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

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

}
于 2015-07-01T20:29:49.633 回答