问题
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?
UPDATE
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.
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/31168657/how-can-i-respond-to-a-keyboard-chord-and-replace-the-entered-alpha-key-with-a-s