2

I have an event handling a keypress (KeyDown) that has KeyEventArgs argument. This class has a few relevant members:

  • KeyCode
  • KeyData
  • KeyValue

My problem is: Whenever I press BackSlash, I'd like to get a String with the single character "\".

Whenever I call ToString() on any of the above characters, it just returns Oem5.

I know I could just throw it in a giant if clause, but I'd like to avoid that if I can.

Thanks!

greggorob64
  • 2,487
  • 2
  • 27
  • 55

1 Answers1

3

Keyboard Mapping in .NET provides an answer, though it may be easier to use the KeyPress event than KeyDown. KeyDown works at the level of abstraction of the actual keyboard (bunch of keys, will differ according to locale and set-up and may have e.g. a two different sets of numbers, which you may wish to distinguish, and there may not even be a character if the key is a modifier or dead-accent key). KeyPress is better-equipped to deal with the level of abstraction of actual characters being input.

Community
  • 1
  • 1
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • KeyPress ended up working best off all the answers. KeyChar had exactly what I was looking for. It is a pain in the butt through, because it doesn't handle function keys. I have to use a compbination of both keypress and keydown to handle all of my keys now. Thanks! – greggorob64 Mar 23 '11 at 14:47