2

On different machines, in an OnKeyDown override, the e.Key values are not necessarily consistent. For instance, on my Mac running Win7-64 under Boot Camp, the backslash key is reported as Key.OemQuote and not Key.OemBackslash as one would expect, which makes no sense considering that's not even on the same key. On a co-worker's Dell, it's reported as something else entirely.

However, in both cases, the correct backslash character is appearing in a textbox when typing so the OS obviously somehow knows that's what character that key is tied to.

We're trying to determine if, given a keycode on a particular system, you can get the corresponding character, if any, which that key represents (assuming no modifiers are pressed.)

Note: I am already aware of the OnPreviewTextInput and OnTextInput events which deal with the characters that are typed, but that is not what I am asking.

I am specifically asking about a function that takes a KeyCode as a parameter and returns what character would be displayed on-screen if that key were to be pressed.

For instance, on my machine, the function would take Key.OemQuote and would return the backslash character. Passing in Key.Control would return null (since nothing actually appears when Control is pressed alone.)

Note: I've put up another question about dealing with the different key codes specifically that may look similar to this question, but this is a very specific separate question relating to a function that takes such a code and returns a character, so please do not mark this as a duplicate. It is not.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • I think [this guy](http://stackoverflow.com/a/5826175/1069200) have figured it out. All my tests using his code passed, dont know about on a Mac. – Johan Larsson Oct 25 '12 at 00:03
  • Put that in an answer, instead of a comment, and if it works, I'll mark yours as the accepted one. Looks like it will. Hate pinvoking, but it does seem to be the only way. – Mark A. Donohoe Oct 25 '12 at 02:35

1 Answers1

0

I think the answer in this thread applies.

Also check out MapVirtualKey PInvoke

Community
  • 1
  • 1
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88