0

I have an issue in a program where I am detecting key presses. All characters are working on QUERTY keyboards but I am aware of issues with certain characters on AZERTY keyboards.

For example, when \ is pressed on an AZERTY keyboard, it is given the keycode of 226 (â).

How can I ensure that the behaviour is consistent across keyboard layouts? I expected the inbuilt event handling to be consistent.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alan Spark
  • 8,152
  • 8
  • 56
  • 91
  • You might be able to check the key layout. I don't know the function. Question: does any one use google anymore? – Cole Tobin May 21 '12 at 20:27
  • show us how you are getting hold of the key presses – David Heffernan May 21 '12 at 20:38
  • I am using the standard KeyDown event of the RichTextBox and checking e.KeyCode. – Alan Spark May 21 '12 at 20:45
  • I have just noticed that the KeyPress event works as expected. However, I need to use the KeyDown event for my processing :( – Alan Spark May 22 '12 at 07:34
  • For anyone still stumbling on this now, the answer of how to accurately get a character from a key code is given [here](https://stackoverflow.com/q/6929275/395685). I'm not gonna mark a decade old question as duplicate though. – Nyerguds Feb 25 '21 at 22:34

3 Answers3

0

You might want to first detect the keyboard layout and then use a different keycode to character mapping.

Community
  • 1
  • 1
Ani
  • 10,826
  • 3
  • 27
  • 46
0

I cannot reproduce that at all. Backslash is composed on an AZERTY keyboard by holding down AltGr and then pressing the 8 key on the row of numbers above the letters. When you do that the following KeyDown events are fired:

  • KeyCode: Menu, KeyValue: 18 for the AltGr going down
  • KeyCode: D8, KeyValue: 56 for the 8 going down

On the other hand, if you press the key immediately to the left of W then a KeyDown with KeyCode OemBackslash and KeyValue 226 is fired, and a < character appears in the text box.

I wonder if your keyboard layout is not set correctly to match your particular keyboard. I'm assuming that your keyboard looks like the image on this Wikipedia page: http://en.wikipedia.org/wiki/AZERTY

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Yes, different keyboard layouts give different key codes. The documentation describes VK_OEM_102 (keycode 226) as "<>" or "\|" on RT 102-key keyboard, so the keycode isn't even supposed to correspond to a single particular character.

To see which character a key corresponds to, use the MapVirtualKey function. You can then check whether it's a backslash.