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:
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 (keyData == (Keys.Control | Keys.E))
{
replacement = useHTMLCodes ? "é" : "é";
}
else if (keyData == (Keys.Control | Keys.Shift | Keys.E))
{
replacement = useHTMLCodes ? "É" : "É";
}
if (keyData == (Keys.Control | Keys.I))
{
replacement = useHTMLCodes ? "í" : "í";
}
else if (keyData == (Keys.Control | Keys.Shift | Keys.I))
{
replacement = useHTMLCodes ? "Í" : "Í";
}
if (keyData == (Keys.Control | Keys.O))
{
replacement = useHTMLCodes ? "ó" : "ó";
}
else if (keyData == (Keys.Control | Keys.Shift | Keys.O))
{
replacement = useHTMLCodes ? "Ó" : "Ó";
}
if (keyData == (Keys.Control | Keys.U))
{
replacement = useHTMLCodes ? "ú" : "ú";
}
else if (keyData == (Keys.Control | Keys.Shift | Keys.U))
{
replacement = useHTMLCodes ? "Ú" : "Ú";
}
if (keyData == (Keys.Control | Keys.Alt | Keys.U))
{
replacement = useHTMLCodes ? "ü" : "ü";
}
else if (keyData == (Keys.Control | Keys.Alt | Keys.Shift | Keys.U))
{
replacement = useHTMLCodes ? "Ü" : "Ü";
}
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)) {
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.