Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Allow Hot Key Combinations to Insert Specified Characters Into a Textbox (C#/Winforms)

0.00/5 (No votes)
2 Jul 2015 1  
Set up hotkey combinations to insert characters you specify, such as accented characters or other special characters, into a textbox

Saccharine Dwarf / Dulce Enano

This will be short and sweet: If you want to enter hot keys while in a textbox to quickly enter accented characters (rather than install and set up a special keyboard, or memorize "Ins+01789342" or whatever), use the following code as a starting point to accomplish this:

// Individual combinations won't work where you already have the same hotkey combination assigned elsewhere; e.g., if you have "Ctrl+Shift+N" assigned to open Notepad (or whatever), that particular combination will not work here. So: YMMV / caveat lector.
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 ? "Ñ" : "Ñ"; 
        }
        // ?
        if (keyData == (Keys.Control | Keys.OemQuestion))
        {
            replacement = useHTMLCodes ? "¿" : "¿";
        }
        // !
        if (keyData == (Keys.Control | Keys.D1)) // The exclamation point is "D1"...?!?
        {
            replacement = useHTMLCodes ? "¡" : "¡";
        }

        if (replacement != "")
        {
            tb.SelectedText = replacement;
            return true;
        }
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

In this case, the code is for Spanish characters (accents, the beginning exclamation mark and question mark, etc.). You can obviously adapt the idea to your needs. I like it because it's an intuitive way to create accented characters (simply mash the "Ctrl" key along with it, such as Ctrl+E for the "é" character; and then to create the capitalized version of the character, sprinkle in the Shift key).

Note: To get this code to compile, you need to add a checkbox to your form named "checkBoxUseHTMLCodes" - or strip that part out of the code, if you don't need it.

Note also that the code above does not display correctly; the HTML codes are shown in both cases/places (it should be the accented or special char in the second instance each time). You need to download the attached file to see the code as it should be seen.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here