3 using Microsoft.Xna.Framework.Input;
4 using System.Collections.Generic;
9 public sealed
class KeyboardTextBuffer:
System.Windows.Forms.IMessageFilter
12 private enum KeyModifiers :
int 26 private struct KeyData
29 public KeyModifiers Modifier;
32 StringBuilder textData =
new StringBuilder();
33 Stack<KeyData> keyData =
new Stack<KeyData>();
34 KeyModifiers modifier;
37 public bool Enabled {
get;
set; }
38 public bool TranslateMessage {
get;
set; }
40 public bool TabEnabled {
get;
set; }
41 public bool BackspaceEnabled {
get;
set; }
42 public bool MultilineEnabled {
get;
set; }
44 public String Text {
get {
return textData.ToString(); } }
45 public int TextLength {
get {
return keyData.Count; } }
47 public event Action TextChanged;
49 private bool inTextChanged;
50 private void OnTextChanged()
52 if ( inTextChanged || TextChanged == null )
return;
55 inTextChanged =
false;
58 public KeyboardTextBuffer()
60 System.Windows.Forms.Application.AddMessageFilter(
this );
61 TranslateMessage =
true;
70 #region IMessageFilter 102 [
System.Runtime.InteropServices.DllImport(
"user32.dll", EntryPoint =
"TranslateMessage" )]
103 protected extern static bool _TranslateMessage( ref
System.Windows.Forms.Message m );
107 bool System.Windows.Forms.IMessageFilter.PreFilterMessage( ref
System.Windows.Forms.Message m )
116 if ( (m.LParam.ToInt32() & (1 << 30)) == 0 )
119 switch ( (Vk)m.WParam )
122 if ( (m.LParam.ToInt32() & (1 << 24)) == 0 )
124 data =
new KeyData {
Key = Keys.LeftControl, Modifier = modifier };
125 keyData.Push( data );
126 modifier |= KeyModifiers.LeftControl;
130 data =
new KeyData {
Key = Keys.RightControl, Modifier = modifier };
131 keyData.Push( data );
132 modifier |= KeyModifiers.RightControl;
136 if ( (m.LParam.ToInt32() & (1 << 24)) == 0 )
138 data =
new KeyData {
Key = Keys.LeftAlt, Modifier = modifier };
139 keyData.Push( data );
140 modifier |= KeyModifiers.LeftAlt;
144 data =
new KeyData {
Key = Keys.RightAlt, Modifier = modifier };
145 keyData.Push( data );
146 modifier |= KeyModifiers.RightAlt;
150 if ( (m.LParam.ToInt32() & (1 << 24)) == 0 )
152 data =
new KeyData {
Key = Keys.LeftShift, Modifier = modifier };
153 keyData.Push( data );
154 modifier |= KeyModifiers.LeftShift;
158 data =
new KeyData {
Key = Keys.RightShift, Modifier = modifier };
159 keyData.Push( data );
160 modifier |= KeyModifiers.RightShift;
165 data =
new KeyData {
Key = (Keys)m.WParam, Modifier = modifier };
166 keyData.Push( data );
171 if ( TranslateMessage )
172 _TranslateMessage( ref m );
180 switch ( (Vk)m.WParam )
183 if ( (m.LParam.ToInt32() & (1 << 24)) == 0 )
184 modifier &= ~KeyModifiers.LeftControl;
186 modifier &= ~KeyModifiers.RightControl;
189 if ( (m.LParam.ToInt32() & (1 << 24)) == 0 )
190 modifier &= ~KeyModifiers.LeftAlt;
192 modifier &= ~KeyModifiers.RightAlt;
195 if ( (m.LParam.ToInt32() & (1 << 24)) == 0 )
196 modifier &= ~KeyModifiers.LeftShift;
198 modifier &= ~KeyModifiers.RightShift;
205 char c = (char)m.WParam;
207 && !( TabEnabled && c ==
'\t' )
208 && !( MultilineEnabled && ( c ==
'\r' || c ==
'\n' ) )
209 && !( BackspaceEnabled && c ==
'\b' ) )
215 if ( c ==
'\b' && textData.Length > 0 && textData[textData.Length - 1] !=
'\b' )
221 textData.Append( c );
227 if ( ((
int)m.WParam & 0xffff) == (
int)Wa.Inactive )
229 modifier = KeyModifiers.None;