0

I have to detect whether the key being pressed in a control is a printable character in any language or not. I was trying to create a if statement but it is just too long to be sensible. So how can it be done?

if(e.KeyCode != Keys.Escape | e.KeyCode != Keys.Enter |  e.KeyCode != Keys.Enter |e.KeyCode != Keys.LShiftKey | e.KeyCode != Keys.RShiftKey | e.KeyCode != Keys.LMenu | e.KeyCode != Keys.RMenu | e.KeyCode != Keys.LWin | e.KeyCode != Keys.RWin | e.KeyCode != Keys.Apps | e.KeyCode != Keys.Alt | e.KeyCode != Keys.Shift | e.KeyCode != Keys.Control | e.KeyCode != Keys.Attn | e.KeyCode != Keys.Capital | \ e.KeyCode != Keys.CapsLock | e.KeyCode != Keys.ControlKey | e.KeyCode != Keys.Crsel |e.KeyCode != Keys.End | e.KeyCode != Keys.EraseEof | e.KeyCode != Keys.Execute | e.KeyCode != Keys.F1 | e.KeyCode != Keys.F2 | e.KeyCode != Keys.F3 | e.KeyCode != Keys.F4 | e.KeyCode != Keys.F5 | e.KeyCode != Keys.F6 |e.KeyCode != Keys.F7 | e.KeyCode != Keys.F8 | e.KeyCode != Keys.F9 | e.KeyCode != Keys.F10 | e.KeyCode != Keys.F11 | e.KeyCode != Keys.F12 | e.KeyCode != Keys.F13 | e.KeyCode != Keys.F14| e.KeyCode != Keys.F15 | e.KeyCode != Keys.F16 | e.KeyCode != Keys.F17 | e.KeyCode != Keys.F18 | e.KeyCode != Keys.F19 | e.KeyCode != Keys.F20 | e.KeyCode != Keys.F21 | e.KeyCode != Keys.F22 | e.KeyCode != Keys.F23 | e.KeyCode != Keys.F24 | e.KeyCode != Keys.HanguelMode | e.KeyCode != Keys.HangulMode | e.KeyCode != Keys.HanjaMode)

The above code just doesn't make any sense to me as the most logical way of handling the input, so ideas please?

  • I think you can use [this question](https://stackoverflow.com/questions/318777/c-sharp-how-to-translate-virtual-keycode-to-char) to find an answer. – NetMage Apr 16 '20 at 19:07
  • @NetMage Sorry, those answers did not give me an answer to my question. – Dave Gordon Apr 16 '20 at 21:21
  • If you use the `KeysToUnicode` method from [this answer](https://stackoverflow.com/a/38787314/2557128) and see if you get a translation, that should be most of what you need, I think? A Key may or may not be a printable character depending on the state of the keyboard, there really isn't a simple answer, but this is close. – NetMage Apr 16 '20 at 21:35
  • Though it does look like the call to `GetKeyboardState` could cause timing issues - unless you already happen to know if the Shift, etc. keys were up or down when the key was pressed. – NetMage Apr 16 '20 at 23:20

0 Answers0