2

In our WPF application, we were wondering why our CTRL-Backslash keyboard shortcut wasn't firing. Wrote a quick test app to log what key was being pressed, and in the OnKeyDown override, e.Key was being reported as OemQuote and not OemBackslash as expected.

Worse, on someone else's machine (but also with a US keyboard and the US layout too, but a different manufacturer), they get a different value for e.Key altogether.

Yes, I know that's what 'Oem' stands for, but shouldn't all OEMs know what the heck a backslash is and treat it consistently?!

If not, which is the case, how are you supposed to be able to assign Backslash to a keyboard shortcut for different machines?

Now I know the OS knows it's a backslash because on both machines, the correct value is typed into a textbox, so obviously the OS (through the driver I'm assuming) knows what to map it to, but I can't believe that I can't create a shortcut based on the backslash and expect it to work reliably on different machines and configurations.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Oem5 is the normal virtual key code, 0xdc. There's no hint in your question to suggest why that's not the case on your keyboard. The physical location of the key on the keyboard varies a great deal, that makes it in general a poor choice for a shortcut key. And never assume a virtual key matches a typing key or the caption on the key itself. – Hans Passant Oct 24 '12 at 22:53
  • But you have to assume somewhat. After all, that's the code you have to use for a keyboard shortcut since they are key-based, not character-based even though to a user, the shortcut is character-based, not key-based. You don't say CTRL-0xDC, you say Ctrl-Backspace. If that doesn't always map to OemBackspace, then they shouldn't give it such a name because it's misleading. Why name it in the first place? Regardless, I think my solution will be to look up which key is represented by the backspace, pinvoking the win32 API, then manually map it in an InitCommands statement. That may work. – Mark A. Donohoe Oct 25 '12 at 02:37
  • No, you can't say Ctrl+Backslash. Your Japanese customer will have to press Ctrl+Yen. You're fighting 25 years of evolution of the type-writer layout and culture. Ever seen a type-writer? Why do you keep talking about backspace btw? – Hans Passant Oct 25 '12 at 08:12
  • You've just proved my point. If that's Japanese Yen, then it shouldn't be called Backslash, and if you're basing keyboard shortcuts on what you have to tell the user, then they shouldn't be using key codes but rather characters. And the backspace was obviously a typo. – Mark A. Donohoe Oct 28 '12 at 13:01
  • And that's why it is not called "Backslash" but Oem5. – Hans Passant Oct 28 '12 at 13:17
  • I have to call you on that. Look at the help file. There *is* an entry called OemBackslash contrary to your statement. That was the very first thing I said... that OemBackslash does *not* match the backslash key, which, as you pointed out, is reported as something else, but even that differs on different machines even though both are US keboards. Again, you're proving my point that you can't use this metaphor to assign shortcut keys. For instance, letters are in different locations on a French keyboard, but you can still specify those by letters. You can't with the backslash key. – Mark A. Donohoe Nov 14 '12 at 06:50
  • @HansPassant I think I've found why no one will ever get OemBackslash on current PCs, can you shed some of your expertise on the subject ? http://stackoverflow.com/a/22861921/361899 Thank you. – aybe Apr 05 '14 at 16:39
  • 1
    @Aybe - Well, "no one" is a strong call, I use a keyboard every day that still has the key. It is old. He's not easily convinced, is he :) – Hans Passant Apr 05 '14 at 16:49
  • Yeah, he's hard to convince but hopefully with time he'll be one of us :D – aybe Apr 05 '14 at 17:30
  • Nope! Still not convinced! :) Been doing Mac/iOS development for a year now and these types of issues are non-existent on the Mac. We can easily connect a backslash key that works consistently. Not so on Windows with WPF. And I re-read this entire thread and still stand by my original sentiment that shortcuts should be based on key, not key code as that would handle all international cases as well, especially since you can set multiple bindings for the same command for when languages differ (i.e. Japan.) Add in user-acing configurations and it becomes a no-brainer. – Mark A. Donohoe Apr 17 '17 at 19:21
  • Just chiming in on my old points, literally from years ago. Here's why I still think MS got it wrong. When you define a shortcut, it is based on what the user sees. If you say CTRL-P, it has nothing to do with where the CTRL or P keys are on the keyboard. It's saying 'On your keyboard, if you press the ones labeled CTRL and P I will do this!' That's why IMHO, the shortcuts should be defined based on the keycaps, not the physical keys as reported by the driver. There shouldn't be OEMBACKSLASH, there should simply be BACKSLASH the same as P and the system should map that as needed. – Mark A. Donohoe Jan 30 '20 at 18:43
  • In the above, even if three different keyboards from three different countries have that key located in different locations, they would all work. Yes, there is the problem still that the backslash on some keyboards may only be reached by, say ALT-Backslash, in which case CTRL-Backslash for them would actually be CTRL-ALT-BACKSLASH, but cumbersome as that may be, it would at least be consistent. That's why I feel is wrong with it's current implementation. It's not consistent. It's too hardware/language/country-specific. Make sense? – Mark A. Donohoe Jan 30 '20 at 18:47

0 Answers0