0

OEM keys depends on the keyboard, that's ok. What I'm trying to accomplish is what Visual Studio does for a few shortcuts.

To focus on Solution Explorer, my shortcut is CTRL+ç (I'm using a Brazilian keyboard).

So, I've created a KeyGesture with Key.Oem1 and the shortcut works just fine, however WPF shows CTRL+Oem1 in the InputGestureText. I want to show the actually character bound to Oem1 key.

var gesture = new KeyGesture(Key.Oem1, ModifierKeys.Control);
var text = gesture.GetDisplayStringForCulture(new CultureInfo("pt-BR"));
//'text' is CTRL+Oem1 and should be CTRL+ç

How can I get the actual character? I did find this but it uses WinForm Keys enum.

JobaDiniz
  • 862
  • 1
  • 14
  • 32
  • 1
    Just use your code from the snippet that uses the WinForm Keys enum. It's just using those to compare against modifier key state, and you don't care about those boolean outputs. So rip that code out, and you won't have any need for a WinForm reference. – Lynn Crumbling Jun 21 '17 at 12:22
  • That snippet does assume an input of a Virtual Key Code, though. Does KeyGesture give you that? – Lynn Crumbling Jun 21 '17 at 12:23
  • `WinAPI.ToUnicode((uint) keys, 0, keyboardState, buf, 256, 0);` WinForms Keys enum values are different from WPF Key enum values. – JobaDiniz Jun 21 '17 at 12:24
  • I never said that they weren't. The function takes a virtual key code as a uint, not a WinForm enum type. – Lynn Crumbling Jun 21 '17 at 12:25
  • yes, but the code just cast the Keys enum, and if I cast the WPF Key enum, it will be a different value. – JobaDiniz Jun 21 '17 at 12:26
  • 1
    So just use the KeyInterop Class to convert the WPF Key -> Virtual Key code: https://msdn.microsoft.com/en-us/library/system.windows.input.keyinterop.aspx – Lynn Crumbling Jun 21 '17 at 12:26
  • `KeyInterop` is the answer, thanks. It's a bit sad that WPF prints Ctrl+Oem1... MenuItem command (with keygesture) binding will by default show Ctrl+Oem1; now I have to override that behavior setting `InputGestureText` manually. – JobaDiniz Jun 21 '17 at 13:11
  • Terrific! Glad to hear you've got a solution :) – Lynn Crumbling Jun 21 '17 at 15:23

0 Answers0