Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Jul 2015CPOL1 min read 7.1K   35   2  
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:

C#
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --