マッピングに必要なすべてのエントリを保持する辞書を定義し、次のKeyPress
ようにイベントを処理できます。
Dictionary<char,char> dict = new Dictionary<char,char>();
//Initialize your dict, this should be done somewhere in your form constructor
dict['L'] = 'N';
dict['A'] = 'B';
//....
//KeyPress event handler for your textBox1
private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
char c;
if(dict.TryGetValue(e.KeyChar, out c)) e.KeyChar = c;
}
注:ユーザーが不要なテキストを貼り付けないようにする場合は、メッセージをキャッチして、WM_PASTE
クリップボードからテキストを取得し、次のように修正したテキストをクリップボードに戻すことができます。
public class NativeTextBox : NativeWindow {
public Dictionary<char, char> CharMappings;
public NativeTextBox(Dictionary<char,char> charMappings){
CharMappings = charMappings;
}
protected override void WndProc(ref Message m) {
if (m.Msg == 0x302){ //WM_PASTE
string s = Clipboard.GetText();
foreach (var e in CharMappings){
s = s.Replace(e.Key, e.Value);
}
Clipboard.SetText(s);
}
base.WndProc(ref m);
}
}
//Then hook it up like this (place in your form constructor)
new NativeTextBox(dict).AssignHandle(textBox1.Handle);