如何响应键盘和弦并将输入的阿尔法键替换为特殊键

本文关键字:阿尔法 输入 替换 何响应 响应 键盘 | 更新日期: 2023-09-27 18:00:45

我想在使用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产生,以及"¡",因为似乎没有对应于"!"的键成员,我可以尝试切换。

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