Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify if a event Key is text (not only alphanumeric)

I've a textbox with an event that should do things when some text is entered. It's easy to check if it is alphanumeric as stated here Can I determine if a KeyEventArg is an letter or number? :

if ( ( ( e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z ) ||
( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) ||
( e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 ) )

The problem with this approach is that I should also check manually for -?!¿[]() with Key.OemMinus, Key.OemQuestion, etc.

Is there some way to check if it's a text keystroke or I should check manually (which is not very elegant in my opinion)?

like image 402
David Fornas Avatar asked Aug 09 '12 10:08

David Fornas


1 Answers

As no other option is suggested I used the following code to allow nearly all text keystrokes. Unfortunatelly, this is keyboard dependant so it's not very elegant. Hopefully is not a critical aspect in the application, it's only a matter of usability.

bool isText = (e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
            || e.Key == Key.OemQuestion || e.Key == Key.OemQuotes || e.Key == Key.OemPlus || e.Key == Key.OemOpenBrackets || e.Key == Key.OemCloseBrackets || e.Key == Key.OemMinus
             || e.Key == Key.DeadCharProcessed || e.Key == Key.Oem1 || e.Key == Key.Oem7 || e.Key == Key.OemPeriod || e.Key == Key.OemComma || e.Key == Key.OemMinus
              || e.Key == Key.Add || e.Key == Key.Divide || e.Key == Key.Multiply || e.Key == Key.Subtract || e.Key == Key.Oem102 || e.Key == Key.Decimal;
like image 186
David Fornas Avatar answered Oct 13 '22 00:10

David Fornas