How to determine if a KeyEventArgs contains a "text" character?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a control which extends System.Windows.Forms.TextBox and adds in
the concept of EditMode vs. ReadyMode (similar to an Excel cell).

So normally when focus arrives on the control, I hide the blinking cursor
and change the mouse pointer from an I-beam to an arrow. When the user hits
certain keys, the control goes into Edit mode (for instance if they type a
letter), and the letter gets appended to the end of the string. When they're
done typing they can hit <Enter> to commit the change and go back to Ready
mode, or <Esc> to cancel the change, revert to the old Text, and go back to
Ready mode.

Unfortunately, I have to do this by hooking the KeyDown event and testing
the e.KeyCode value. However that means going through the entire list of
possible values for the System.Windows.Forms.Keys enumeration, which includes
some crazy keys like Keys.IMEAceept (sic) and Keys.Oem8 (different
manufacturers have their own '8' key?).

Is there some function in the Framework or WindowsAPI that I can call which
will tell me if a certain KeyCode ought to cause some text to appear on the
screen?

Thanks
 
For anyone who's interested, I coded a kind of ugly but workable hack for this:

_isTextKey = new Hashtable();

_isTextKey[Keys.Modifiers] = false;
_isTextKey[Keys.None] = false;
_isTextKey[Keys.LButton] = false;
_isTextKey[Keys.RButton] = false;
_isTextKey[Keys.Cancel] = false;
_isTextKey[Keys.MButton] = false;
_isTextKey[Keys.XButton1] = false;
_isTextKey[Keys.XButton2] = false;
_isTextKey[Keys.Back] = false;
_isTextKey[Keys.Tab] = false;
_isTextKey[Keys.LineFeed] = false;
_isTextKey[Keys.Clear] = false;
_isTextKey[Keys.Enter] = false;
_isTextKey[Keys.Return] = false;
_isTextKey[Keys.ShiftKey] = false;
_isTextKey[Keys.ControlKey] = false;
_isTextKey[Keys.Menu] = false;
_isTextKey[Keys.Pause] = false;
_isTextKey[Keys.Capital] = false;
_isTextKey[Keys.CapsLock] = false;
_isTextKey[Keys.HanguelMode] = false;
_isTextKey[Keys.HangulMode] = false;
_isTextKey[Keys.KanaMode] = false;
_isTextKey[Keys.JunjaMode] = false;
_isTextKey[Keys.FinalMode] = false;
_isTextKey[Keys.HanjaMode] = false;
_isTextKey[Keys.KanjiMode] = false;
_isTextKey[Keys.Escape] = false;
_isTextKey[Keys.IMEConvert] = false;
_isTextKey[Keys.IMENonconvert] = false;
_isTextKey[Keys.IMEAceept] = false;
_isTextKey[Keys.IMEModeChange] = false;
_isTextKey[Keys.Space] = false;
_isTextKey[Keys.PageUp] = false;
_isTextKey[Keys.Prior] = false;
_isTextKey[Keys.Next] = false;
_isTextKey[Keys.PageDown] = false;
_isTextKey[Keys.End] = false;
_isTextKey[Keys.Home] = false;
_isTextKey[Keys.Left] = false;
_isTextKey[Keys.Up] = false;
_isTextKey[Keys.Right] = false;
_isTextKey[Keys.Down] = false;
_isTextKey[Keys.Select] = false;
_isTextKey[Keys.Print] = false;
_isTextKey[Keys.Execute] = false;
_isTextKey[Keys.PrintScreen] = false;
_isTextKey[Keys.Snapshot] = false;
_isTextKey[Keys.Insert] = false;
_isTextKey[Keys.Delete] = false;
_isTextKey[Keys.Help] = false;
_isTextKey[Keys.D0] = true;
_isTextKey[Keys.D1] = true;
_isTextKey[Keys.D2] = true;
_isTextKey[Keys.D3] = true;
_isTextKey[Keys.D4] = true;
_isTextKey[Keys.D5] = true;
_isTextKey[Keys.D6] = true;
_isTextKey[Keys.D7] = true;
_isTextKey[Keys.D8] = true;
_isTextKey[Keys.D9] = true;
_isTextKey[Keys.A] = true;
_isTextKey[Keys.B] = true;
_isTextKey[Keys.C] = true;
_isTextKey[Keys.D] = true;
_isTextKey[Keys.E] = true;
_isTextKey[Keys.F] = true;
_isTextKey[Keys.G] = true;
_isTextKey[Keys.H] = true;
_isTextKey[Keys.I] = true;
_isTextKey[Keys.J] = true;
_isTextKey[Keys.K] = true;
_isTextKey[Keys.L] = true;
_isTextKey[Keys.M] = true;
_isTextKey[Keys.N] = true;
_isTextKey[Keys.O] = true;
_isTextKey[Keys.P] = true;
_isTextKey[Keys.Q] = true;
_isTextKey[Keys.R] = true;
_isTextKey[Keys.S] = true;
_isTextKey[Keys.T] = true;
_isTextKey[Keys.U] = true;
_isTextKey[Keys.V] = true;
_isTextKey[Keys.W] = true;
_isTextKey[Keys.X] = true;
_isTextKey[Keys.Y] = true;
_isTextKey[Keys.Z] = true;
_isTextKey[Keys.LWin] = false;
_isTextKey[Keys.RWin] = false;
_isTextKey[Keys.Apps] = false;
_isTextKey[Keys.NumPad0] = true;
_isTextKey[Keys.NumPad1] = true;
_isTextKey[Keys.NumPad2] = true;
_isTextKey[Keys.NumPad3] = true;
_isTextKey[Keys.NumPad4] = true;
_isTextKey[Keys.NumPad5] = true;
_isTextKey[Keys.NumPad6] = true;
_isTextKey[Keys.NumPad7] = true;
_isTextKey[Keys.NumPad8] = true;
_isTextKey[Keys.NumPad9] = true;
_isTextKey[Keys.Multiply] = true;
_isTextKey[Keys.Add] = true;
_isTextKey[Keys.Separator] = true;
_isTextKey[Keys.Subtract] = true;
_isTextKey[Keys.Decimal] = true;
_isTextKey[Keys.Divide] = true;
_isTextKey[Keys.F1] = false;
_isTextKey[Keys.F2] = false;
_isTextKey[Keys.F3] = false;
_isTextKey[Keys.F4] = false;
_isTextKey[Keys.F5] = false;
_isTextKey[Keys.F6] = false;
_isTextKey[Keys.F7] = false;
_isTextKey[Keys.F8] = false;
_isTextKey[Keys.F9] = false;
_isTextKey[Keys.F10] = false;
_isTextKey[Keys.F11] = false;
_isTextKey[Keys.F12] = false;
_isTextKey[Keys.F13] = false;
_isTextKey[Keys.F14] = false;
_isTextKey[Keys.F15] = false;
_isTextKey[Keys.F16] = false;
_isTextKey[Keys.F17] = false;
_isTextKey[Keys.F18] = false;
_isTextKey[Keys.F19] = false;
_isTextKey[Keys.F20] = false;
_isTextKey[Keys.F21] = false;
_isTextKey[Keys.F22] = false;
_isTextKey[Keys.F23] = false;
_isTextKey[Keys.F24] = false;
_isTextKey[Keys.NumLock] = false;
_isTextKey[Keys.Scroll] = false;
_isTextKey[Keys.LShiftKey] = false;
_isTextKey[Keys.RShiftKey] = false;
_isTextKey[Keys.LControlKey] = false;
_isTextKey[Keys.RControlKey] = false;
_isTextKey[Keys.LMenu] = false;
_isTextKey[Keys.RMenu] = false;
_isTextKey[Keys.BrowserBack] = false;
_isTextKey[Keys.BrowserForward] = false;
_isTextKey[Keys.BrowserRefresh] = false;
_isTextKey[Keys.BrowserStop] = false;
_isTextKey[Keys.BrowserSearch] = false;
_isTextKey[Keys.BrowserFavorites] = false;
_isTextKey[Keys.BrowserHome] = false;
_isTextKey[Keys.VolumeMute] = false;
_isTextKey[Keys.VolumeDown] = false;
_isTextKey[Keys.VolumeUp] = false;
_isTextKey[Keys.MediaNextTrack] = false;
_isTextKey[Keys.MediaPreviousTrack] = false;
_isTextKey[Keys.MediaStop] = false;
_isTextKey[Keys.MediaPlayPause] = false;
_isTextKey[Keys.LaunchMail] = false;
_isTextKey[Keys.SelectMedia] = false;
_isTextKey[Keys.LaunchApplication1] = false;
_isTextKey[Keys.LaunchApplication2] = false;
_isTextKey[Keys.OemSemicolon] = true;
_isTextKey[Keys.Oemplus] = true;
_isTextKey[Keys.Oemcomma] = true;
_isTextKey[Keys.OemMinus] = true;
_isTextKey[Keys.OemPeriod] = true;
_isTextKey[Keys.OemQuestion] = true;
_isTextKey[Keys.Oemtilde] = true;
_isTextKey[Keys.OemOpenBrackets] = true;
_isTextKey[Keys.OemPipe] = true;
_isTextKey[Keys.OemCloseBrackets] = true;
_isTextKey[Keys.OemQuotes] = true;
_isTextKey[Keys.Oem8] = false;
_isTextKey[Keys.OemBackslash] = true;
_isTextKey[Keys.ProcessKey] = false;
_isTextKey[Keys.Attn] = false;
_isTextKey[Keys.Crsel] = false;
_isTextKey[Keys.Exsel] = false;
_isTextKey[Keys.EraseEof] = false;
_isTextKey[Keys.Play] = false;
_isTextKey[Keys.Zoom] = false;
_isTextKey[Keys.NoName] = false;
_isTextKey[Keys.Pa1] = false;
_isTextKey[Keys.OemClear] = true;
_isTextKey[Keys.KeyCode] = false;
_isTextKey[Keys.Shift] = false;
_isTextKey[Keys.Control] = false;
_isTextKey[Keys.Alt] = false;

Hope this helps.
 
Why not use the KeyPress event instead. Then you will only
get printable characters

/claes
 
Well, because I want to have certain (non-printable) keys do certain things,
such as F2 puts the control in Edit mode. So, I need to check in KeyDown not
KeyPress since as you rightly noted, KeyPress only occurs for printable
characters.
 
Back
Top