如何响应键盘和弦并将输入的Alpha键替换为特殊的Alpha键? [英] How can I respond to a keyboard chord and replace the entered alpha key with a special one?

查看:109
本文介绍了如何响应键盘和弦并将输入的Alpha键替换为特殊的Alpha键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

Idle_Mind的答案很好,但是仍然有两个键不起作用-Ñ"(应由Ctrl + Shift + N产生)和¡",因为似乎没有对应的Keys成员到 "!"我可以尝试转变.

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.

推荐答案

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

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

}

这篇关于如何响应键盘和弦并将输入的Alpha键替换为特殊的Alpha键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆